目录
什么是循环

循环结构的特点
循环结构:1.循环条件
2.循环操作
语法:

特点:先判断,后执行
- int count = 1;
- while (count <= 50){
- System.out.println("打印" + count + "份试卷");
- count++;
- }
-
- //如果循环不能退出,会出现什么情况
- //永远不会退出的循环 死循环 在实际开发过程中要避免“死循环”,编写完代码后检查循环是否能够退出
- int i= 0;
- while (i < 4){
- System.out.println("循环一直运行,不会退出");
- i++;
- }



为什么需要do-while?

- import java.util.Scanner;
-
- public class DoWhileDemo02 {
- public static void main(String[] args) {
-
- Scanner input = new Scanner(System.in);
- int i ;
- do{
- System.out.println("上机编写程序!");
- System.out.println("成绩合格了吗");
- //老师判断是否合格
- i = input.nextInt();
- }while(!(i == 1));
- System.out.println("恭喜通过考试");
- }
-
- }

for 比 while更加简洁
语法:
for(初始化循环变量;循环条件;改变循环变量的值){
循环体语句;
}
分号不能省略,for循环中初始化循环变量,循环条件,改变循环变量的值是可以省略的。
案例:

- public static void main(String[] args) {
-
- int score;//每门课的成绩
- int sum = 0;//成绩之和
- double avg = 0.0;//平均分
-
- //定义输入
- Scanner input = new Scanner(System.in);
- System.out.println("请输入学生姓名:");
- String studentName = input.next();
- for (int i=0;i<5;i++){
- System.out.println("请输入5门可中第" + (i + 1)+ "门课程的成绩");
- score = input.nextInt();//录入成绩
- sum = sum + score;//计算成绩和
- }
- //计算平均分
- avg = (double) sum/5;
- System.out.println(studentName+"的平均分是" + avg);
- }
案例:

- package cycle;
-
- import java.util.Scanner;
-
- public class ForDemo4 {
- public static void main(String[] args) {
- int i,j;
- //接受键盘输入
- Scanner input = new Scanner(System.in);
- System.out.println("请输入一个值");
- int val = input.nextInt();
- for(i=0,j=val;i
1;i++,j--){ - System.out.println(i +"+" + j + "="+ (i+j));
- }
- }
- }
常见问题一:

常见问题二:

常见问题3:

常见问题4:
