一、什么是循环
三要素:
1、循环变量初始化
2、循环的判断条件
3、循环体中改变循环变量的值
二、语法结构
int i=0; //循环变量
while (i<10){ //循环条件判断
System.out.println("好好学习");
i++; //改变循环变量的值
}
三、如何使用循环
1、找循环结束的地方
2、找循环变量的初始值
3、找循环变量改变的规律
<strong><span style="font-size:24px;">//do while
import java.util.*;
public 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);
do{
System.out.println("猜猜电脑随机生成的数是多少?");
b=a.nextInt();
if(b<d){
System.out.println("小了");
}else if(b>d){
System.out.println("大了");
}
}while(b!=d);
System.out.println("恭喜答对了");
}
}</span></strong>
<span style="font-size:24px;"><strong>//for
import java.util.*;
public class text2{
public static void main(String[]args){
Scanner a=new Scanner(System.in);
double sum=0;
double ave=0;
for(int i=1;i<=5;i++){
System.out.println("请输入你的第"+i+"门成绩");
int score=a.nextInt();
sum=sum+score;
ave=sum/5;
}
System.out.println("你的平均成绩是:"+ave);
}
}</strong></span>
<span style="font-size:24px;"><strong>//while
import java.util.*;
public class text5{
public static void main(String[]args){
Scanner a=new Scanner(System.in);
System.out.println("请输入你的密码");
while(true){
int password=a.nextInt();
System.out.println("密码不正确请再输入一次");
if(password!=123456){
continue;
}
break;
}
System.out.println("密码正确!");
}
}</strong></span>