Brief discussion on one of the cyclic structures in Java —————— simple application of while
1. What is a cycle?Sometimes you want the code to do the same thing again and again before the work is done. You can use
loops for this purpose. A loop is a programming structure that executes repeatedly when a particular condition (or set of conditions) is met. The loop structure is designed to solve repetitive or regular code.
2, today, a brief talk about the simple application of while
Let's talk about the three elements of the while loop
Three elements of a while loop: (1) Initialize variable (2) loop condition (3) Change the value of the loop variable
When you want to use the while loop to know what these three elements are, it's handy to loop them.
Here is the while loop syntax and features:
Here are a few examples to understand the application of the while loop.
1. Use the example of a running ring first to illustrate
Athletes running on the circular runway, this is a real life cycle of the case.
Three elements are identified in the following code, (1) Initialize the variable count=1, which means running from the first lap (2) loop condition count<=5 means 5 laps to run
(3) Change the cyclic variable change the loop variable count++ run a lap, the number of laps will be added 1. This is a small example of a while loop in real life.
1 PackageCn.happy;2 3 Public classFirstdemorun {4 5 Public Static voidMain (string[] args) {6 //0.1 Initial variables7 intCount = 1;8 //2. Cyclic infrastructure cycle conditions9 while(Count <= 5) {//0.2 Cycle ConditionsTenSystem.out.println ("Running ... This is the "+ Count +" circle); One //0.3. Change the value Acount++; - } - } the}
2. The above example is an example of when a cyclic condition is a numeric value. Another example of a cyclic condition is not a numerical value.
For example: fail the exam, the teacher write code, until the teacher said "can" do not have to write. If the teacher disagrees, he will keep on writing. It's shown in code below.
1 PackageCn.happy;2 ImportJava.util.Scanner;3 Public classfirstdemowhile_02 {4 Public Static voidMain (string[] args) {5 //Fail the exam, write the code, the teacher said "yes" on the Stop6System.out.println ("Teacher, can you?" ");7Scanner input =NewScanner (system.in);8 //0.1 Initial variables9String answer =Input.next ();Ten One while(!answer.equals ("yes")) {//0.2 Cycle Conditions//user input is not "can" A //write code for penalty -System.out.println ("Write Code"); -System.out.println ("Are you Ready?") "); the //0.3 loops traverse the changed place -Answer =Input.next (); - } - } +}
3, when it comes to the loop how variable there are two times, the scheduling and coordination?
There is an example: for example, a training institution, in 2012 when the number of training reached 250,000 people, if the annual rate of 25% growth, then in that year's training
Number of people up to 1 million?
1 PackageCn.happy;2 Public classfirstdemowhile_03 {3 Public Static voidMain (string[] args) {4 intyear=2012;//Initial year//0.1 Initial variables5 Doublepeople=25;//number//0.1 Initial variables6 7 while(people<=100) {//0.2 Cycle Conditions8people=people*1.25;//0.3 cyclic variable change9year++;TenSystem.out.println (year+ "T training number reached" +people); One } A } -}
Analysis of three elements of the variable, (1) The initial variable one is the year 20,121 is the number of people 25, (2) the cycle condition is greater than 100 (3) cyclic variable change people*1.25
4, the following to see such a problem, for 100 or even the sum of
1 Packagecn.happy02;2 Public classDay01 {3 Public Static voidMain (string[] args) {4 //Way One:5 /*int num=2;6 int sum=0;7 While (num<=100) {8 Sum=sum+num;9 num=num+2;Ten } One System.out.println (sum);*/ A - //calculates the sum of "even" loops within 100 - //mode two: Algorithm the intNum=1;//0.1 Initial variables - //define an even number and container - intsum=0;//0.1 Initial variables - while(num<=100) {//0.2 Cycle Conditions + if(num%2==0) {//prove to be an even number -sum=sum+num; + } A //0.3 Cyclic traversal changes atNum=num+1; - } -System.out.println ("and yes" +sum); - } -}
The above code said two ways, mainly about the way two, is in the while loop structure in the face of the loop operation of the value of making a decision is not even.
Brief discussion on one of the cyclic structures in Java —————— simple application of while