4.3 循环语句
4.3.1 循环的定义
当需要反复让某个代码段运行。
4.3.2 循环的分类
for / while / do-while
4.3.3 循环的组成部分
循环的初始值: 循环最初状态
循环的条件: 能够让程序反复运行的条件
循环体: 被反复运行的代码段
迭代: 循环的变更,每次的变化
1 圈<= 10 跑 3
4.3.4 for循环
语法:
for(初始值 ; 循环条件 ; 迭代){ 循环体; }
循环的示例:
for(int i = 1; i <= 100; i ++) { System.out.println("侯军长跑第" + i + "圈"); }
打印1-100之间的偶数:
//写法1 for(int i = 2 ;i <= 100; i += 2) { System.out.println(i); } //写法2 for(int i = 1; i <= 100; i ++) { if(i % 2 == 0) { System.out.println(i); } }
打印1-50 之间敲七数字: 7倍数 或以7结尾
for(int i = 7; i <= 50; i ++) { if(i % 7 == 0 || i % 10 == 7) { System.out.println(i); } }
和/积问题:
求和问题:计算1-100累加和
//1-100累加和 int sum = 0; for(int i = 1; i <= 100;i ++) { sum += i; } System.out.println(sum);
1-n的累计乘积 ,n是控制台接收
//1-n 累计乘 Scanner sc = new Scanner(System.in); System.out.println("请输入n:"); int n = sc.nextInt(); //最后乘积 long mul = 1; //从1开始到n结束 for(int i = 1; i <= n;i ++) { mul = mul * i; } System.out.println(mul);
计数问题: 统计个数
统计一下: 1-100之间有几个 3或7的倍数
int count = 0; for (int i = 1; i <= 100; i++) { if(i % 3 == 0 || i % 7 == 0) { count ++; } } System.out.println(count);//43
统计1900-2022年一共多少个闰年?
int count = 0; for (int i = 1900; i <= 2022; i++) { if(i % 4 == 0 && i % 100 != 0 || i % 400 == 0) count++; } System.out.println(count);
双层循环: 循环的内容循环
//外层 for(int i = 1;i <= 3;i ++){ //内层 for(int j = 1; j <= 3;j ++){ System.out.println(i + "," + j); } } // 1,1 1,2 1,3 // 2,1 2,2 2,3 // 3,1 3,2 3,3
打印:
/** ***** ***** ***** */ //行 for (int i = 1; i <= 3; i++) { //列 for (int j = 1; j <= 5; j++) { System.out.print("*"); } System.out.println(); }
打印:
/** * ** *** **** ***** * *** ***** ******* */ for (int i = 1; i <= 5; i++) { //列 for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } for (int i = 1; i <= 4; i++) { //列 for (int j = 1; j <= 2 * i - 1; j++) { System.out.print("*"); } System.out.println(); } /** * *** ***** ******* */ //行 for (int i = 1; i <= 4; i++) { //空格 for (int k = 1; k <= 4 - i; k++) { System.out.print(" "); } //星星 for (int j = 1; j <= 2 * i - 1; j++) { System.out.print("*"); } System.out.println(); }
for小结:
1, 求和问题 int在for外。
2,
while语句
while语法:
初始值; while(循环条件){ 循环体; 迭代; } e.g. int sum = 0; int i = 1; while( i<= 100 ){ if( i ){ sum += i; // i += 2; i++; } System.out.println(sum); }
注意:
for循环更适用于循环次数固定,shile循环适用于循环条件外部决定,客户需求决定。
e.g.
public class TestWhile5 { public static void main(String[] args) { System.out.println("欢迎进入ATM"); double balance = 5000; while(true) { //System.out.println("于老师18岁"); System.out.println("1.存钱"); System.out.println("2.取钱"); System.out.println("3.查看余额"); System.out.println("4.转账"); System.out.println("5.退出");
死循环总结:
while( true ){
}
for(;;){
}
e.g.
Scanner sc = new Scanner(![img](file:///C:\Users\Pele\AppData\Roaming\Tencent\QQTempSys\`7_{~]GF$3{MOQ4V_}PH]YC.png)System.in); System.out.println("请输入操作编号"); int opr = sc.nextInt(); if(opr == 1) { System.out.println("请输入存款金额"); balance += sc.nextDouble(); }else if(opr == 2) { if( double money <= balance && money >= 100 && money % 100 == 0 ) System.out.println("请输入取款金额"); //判断 balance -= sc.nextDouble(); }else if(opr == 3) { System.out.println("卡内余额:" + balance); }else if(opr == 4) { System.out.println("暂未开启功能"); }else { break; } } System.out.println("谢谢使用"); } }
package com.qf.project07; import java.util.Scanner; public class TestWhile5 { public static void main(String[] args) { System.out.println("欢迎进入ATM"); //账户余额 double balance = 5000; while(true) { //System.out.println("于老师18岁"); System.out.println("1.存钱"); System.out.println("2.取钱"); System.out.println("3.查看余额"); System.out.println("4.转账"); System.out.println("5.退出"); Scanner sc = new Scanner(System.in); System.out.println("请输入操作编号"); int opr = sc.nextInt(); if(opr == 1) { System.out.println("请输入存款金额"); //整百> 0 double money = sc.nextDouble(); if(money % 100 == 0 && money > 0) { balance += money; }else { System.out.println("金额有误"); } }else if(opr == 2) { System.out.println("请输入取款金额"); //判断 double money = sc.nextDouble(); if(money > 0 && money <= balance) { balance -= money; }else { System.out.println("余额不足"); } }else if(opr == 3) { System.out.println("卡内余额:" + balance); }else if(opr == 4) { System.out.println("暂未开启功能"); }else { //退出死循环 break; } } System.out.println("谢谢使用"); } }
4.3.5 while循环
初始值; while(循环条件){ 循环体; 迭代; }
//求和 1-100和 int sum = 0; int a = 1; while(a <= 100) { sum += a; a ++; } System.out.println(sum);
注意: for循环适用于循环次数固定,while循环适用于循环条件外部决定,客户需求决定。
System.out.println("----欢迎进入消消乐游戏大厅-----"); System.out.println("是否开始? 0开始,任意键退出"); Scanner sc = new Scanner(System.in); //初始值 int opr = sc.nextInt(); while(opr == 0) { //循环体 System.out.println("游戏开始进行......"); System.out.println("Amazing......."); System.out.println("游戏结束,是否继续,0继续,任意键退出"); //迭代 opr = sc.nextInt(); } System.out.println("bye-下次继续");
练习:控制台接收学生成绩: 直到接收的成绩是-1 终止接收,获得学生的成绩的总分,最高分和平均分
package com.qf.project07; import java.util.Scanner; public class TestWhile4 { public static void main(String[] args) { System.out.println("----系统录入学生成绩-----"); System.out.println("请输入第1个学生成绩"); Scanner sc = new Scanner(System.in); //初始值 double sum = 0; int count = 0; int max = 0; int score = sc.nextInt(); while(score >= 0) { //循环体 if(score > max) { max = score; } sum += score; count ++; //迭代 System.out.println("请输入第" + (count+1)+ "个学生成绩"); score = sc.nextInt(); } System.out.println("总分数:" + sum); System.out.println("最高分:" + max); System.out.println("平均分:" + (sum / count)); } }
while循环通过死循环制作菜单程序(ATM操作):
package com.qf.project07; import java.util.Scanner; public class TestWhile5 { public static void main(String[] args) { System.out.println("欢迎进入ATM"); //账户余额 double balance = 5000; while(true) { System.out.println("1.存钱"); System.out.println("2.取钱"); System.out.println("3.查看余额"); System.out.println("4.转账"); System.out.println("5.退出"); Scanner sc = new Scanner(System.in); System.out.println("请输入操作编号"); int opr = sc.nextInt(); if(opr == 1) { System.out.println("请输入存款金额"); //整百> 0 double money = sc.nextDouble(); if(money % 100 == 0 && money > 0) { balance += money; }else { System.out.println("金额有误"); } }else if(opr == 2) { System.out.println("请输入取款金额"); //判断 double money = sc.nextDouble(); if(money > 0 && money <= balance) { balance -= money; }else { System.out.println("余额不足"); } }else if(opr == 3) { System.out.println("卡内余额:" + balance); }else if(opr == 4) { System.out.println("暂未开启功能"); }else { //退出死循环 break; } } System.out.println("谢谢使用"); } }
总结死循环:
while(true){
}
for(;;){
}
4.3.6 do-while循环
初始值; do{ 循环体; 迭代; }while(循环条件);
public class TestDoWhile { public static void main(String[] args) { int i = 1 , sum = 0; do { if(i % 2 == 0) { sum += i; } //迭代 i ++; }while(i <= 100); System.out.println(sum); } }
注意:
-
先执行循环体,后判断条件,即使循环条件不满足,也会执行
-
do{} 中的变量不可以使用在while中使用
三种循环的总结:
-
当循环条件中次数固定,首选for , 执行次数: 0 次或n次
-
当循环次数外部决定:
-
while 先判断条件后循环 ,执行次数: 0 次或n次
-
do-while 先执行循环体,后判断条件, 执行次数:1 次或n次
-
三种循环对比:
int i = 1 , sum = 0; for(i = 2; i > 100;i ++) { sum += i; } // while(i > 100) { // sum += i; // i ++; // } // do { // sum += i; // //迭代 // i ++; // }while(i > 100); System.out.println(sum + "," + i);
商品的支付:
public class TestDoWhile3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入商品名称:"); String name = sc.next(); System.out.println("请输入商品价格:"); double price = sc.nextDouble(); System.out.println("请输入商品数量:"); int num = sc.nextInt(); //总额 double sum = price * num * 0.9; System.out.println(name + "总计花费:" + sum); //支付 double money = 0; do { System.out.println("还请支付:" + (sum - money) + "元"); money += sc.nextDouble(); }while(money < sum); //退出找零 System.out.println("支付成功,找零" + (money - sum )); } }
4.3.7 循环的跳出
在循环执行当中,因为某些需要,跳出循环。
语句 | 含义 |
---|---|
continue | 略过当前循环,执行下一个 |
break | 退出当前一层循环 |
return | 退出方法 |
System.exit(0) | 退出虚拟机 |
break 标签; | 标签加在某个循环的上方,从该循环中跳出,向下跳 |
continue 标签 |
for (int i = 1; i <= 10; i++) { if(i == 5) { //continue; //break; //return; //System.exit(0); } System.out.println(i); } System.out.println("循环出来了");
注意:break只能退出一层循环
for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if(i == 2) { break; } System.out.println(i + "," + j); } }
p: for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if(i == 2) { //break; //return; break p; } System.out.println(i + "," + j); } } System.out.println("是否可以运行");