First, what is the cycle
Three elements:
1. Cyclic variable initialization
2, the judgment condition of the cycle
3. Change the value of the cyclic variable in the loop body
Second, the grammatical structure
int i=0; //cyclic variable
while (i<10) {//Loop condition judgment
SYSTEM.OUT.PRINTLN ("Study hard");
i++; //Change the value of the loop variable
}
Third, how to use the loop
1. Where to find the end of the cycle
2, find the initial value of the loop variable
3. Find the law of cyclic variable change
<strong><span style= "FONT-SIZE:24PX;" >//do whileimport java.util.*;p ublic class dowhile{public static void Main (string[] args) {Scanner a=new Scanner ( system.in); int b; Random c=new random (), int d=c.nextint (101);d o{system.out.println ("Guess what the number of randomly generated computers?"). "); B=a.nextint (); if (b<d) {System.out.println (" small ");} else if (b>d) {System.out.println ("big");}} while (B!=d); SYSTEM.OUT.PRINTLN ("Congratulations are correct");}} </span></strong>
<span style= "FONT-SIZE:24PX;" ><strong>//forimport java.util.*;p ublic class text2{public static void Main (String[]args) {Scanner a=new Scanner (system.in);d ouble sum=0;double ave=0;for (int i=1;i<=5;i++) {System.out.println ("Please enter your first" +i+ "Gate score"); int Score=a.nextint (); SUM=SUM+SCORE;AVE=SUM/5;} System.out.println ("Your average score is:" +ave);}} </strong></span>
<span style= "FONT-SIZE:24PX;" ><strong>//whileimport java.util.*;p ublic class text5{public static void Main (String[]args) {Scanner a=new Scanner (system.in); System.out.println ("Please enter your password"); while (true) {int password=a.nextint (); SYSTEM.OUT.PRINTLN ("Password is incorrect please enter again"); if (password!=123456) {continue;} break;} SYSTEM.OUT.PRINTLN ("The password is correct! ");}} </strong></span>
Loops in Java