java第5课-选择结构和循环结构
1. if选择结构
-
代码表示
if (布尔表达式 1){ }else if (布尔表达式 2){ //选择主体 }else if (布尔表达式 3){ //选择主体 }else { //选择主体 } -
注意事项
-
if和else if的区别int a = 5; if (a > 3){//若if判断成立,则不会判断下面的else if语句 System.out.println("a > 3"); }else if (a > 1){ System.out.println("a > 1"); } System.out.println("==========================="); if (a > 3){//每个if语句都进行判断 System.out.println("a > 3"); } if (a > 1){//每个if语句都进行判断 System.out.println("a > 1"); }输出结果:
a > 3 =========================== a > 3 a > 1
-
2. switch选择结构
-
代码表示
switch(value){ case value1: //选择主体 break;//可选 case value2: //选择主体 break;//可选 case value3: //选择主体 break;//可选 //可设置任意数量的case语句 default://可选 //选择主体 } -
注意事项
-
case具有穿透性,所以每个case语句最后一般都需要加
breakScanner scanner = new Scanner(System.in); char grade = scanner.nextLine().charAt(0);//为了解决Java没有nextChar switch (grade) { case 'A': System.out.println("优秀"); case 'B': System.out.println("良好"); case 'C': System.out.println("及格"); case 'D': System.out.println("再接再厉"); default: System.out.println("未知等级"); } scanner.close();输入:
B
输出结果:
良好
及格
再接再厉
未知等级 -
switch 支持字符串String类型
Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); switch (name) { case "小红": System.out.println("优秀"); break; case "小明": System.out.println("良好"); break; case "小东": System.out.println("及格"); break; default: System.out.println("未知等级"); break; } scanner.close();输入:
小红
输出结果:
优秀
-
3. while循环结构
-
代码表示
while(布尔表达式){ //循环主体 } -
注意事项
4. do while循环结构
-
代码表示
do{ //循环主体 }while(布尔表达式) -
注意事项
-
while循环结构是先判断后执行,do while循环结构是先执行后判断 -
do while总是保证循环内容至少被执行一次
-
5. for循环结构
-
代码表示
for(初始化循环控制变量;布尔表达式;更新循环控制变量){ //循环主体 } -
注意事项
-
快捷键 输入:100.for
回车->生成for(int i = 0; i < 100; i++){} -
最先执行初始化步骤,可声明和初始化多个循环控制变量,也可以是空语句
-
执行一次循环后,更新循环控制变量,再次进行循环的判断
-
6. 增强for循环
-
代码表示
for(声明局部变量: 数组){ //循环主体 }以下两个程序等价
-
程序1
int[] numbers = {10,20,30,40,50};//定义了一个数组 for(int x: numbers){//遍历数组的元素 System.out.println(x); } -
程序2
int[] numbers = {10,20,30,40,50};//定义了一个数组 int x; for(int i = 0; i < 5; i++){//遍历数组的元素 x = numbers[i]; System.out.println(x); }输出结果:
10
20
30
40
50
-
-
注意事项
- 增强for循环主要用于数组或集合
- 声明的新局部变量,该变量的数据类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等
- 数组可以是要访问的数组名,也是也是返回值为数组的方法
7. break和continue
-
break和continue的区别-
break可在任何循环语句的循环主体中使用,用来控制循环的流程。break用于强行退出一层循环。 -
continue用在循环主体中,用于跳过某次循环过程,即跳过循环主体中尚未执行的语句,接着进行下一次是否执行循环的判定 -
代码示例:
public class Test { public static void main(String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print("<"+i+","+j+">"); } } System.out.println(""); System.out.println("break强行退出一层循环:"); for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if ((i == 3)&&(j == 1)){ break; } System.out.print("<"+i+","+j+">"); } } System.out.println(""); System.out.println("break跳过一次循环:"); for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if ((i == 3)&&(j == 1)){ continue; } System.out.print("<"+i+","+j+">"); } } } }输出结果:
<1,1><1,2><1,3><2,1><2,2><2,3><3,1><3,2><3,3>
break强行退出一层循环:
<1,1><1,2><1,3><2,1><2,2><2,3>
break跳过一次循环:
<1,1><1,2><1,3><2,1><2,2><2,3><3,2><3,3>
-
-
continue和标签的配合使用-
用于跳转到某个循环中
-
代码示例:
输出100到150之间所有的质数
int count = 0; outer: for (int i = 100; i <= 150; i++){//outer为标签名,标记第一层for循环 for (int j = 2; j < i/2; j++){ if (i%j == 0){ continue outer;//跳转outer标记的第一层for循环,此语句可用break语句代替 } } System.out.printf("%-5d",i);//输出结果左对齐,占5个字符 }输出结果:
101 103 107 109 113 127 131 137 139 149
-
8. for循环结构的简单应用
-
计算0到100之间的奇数的和与偶数的和
public class Test { public static void main(String[] args) { int oddSum = 0; int evenSum = 0; for(int i = 0; i <= 100; i++){ if (i%2 != 0){//奇数 oddSum += i; } if (i%2 == 0){//偶数 evenSum += i; } } System.out.println("奇数的和:"+oddSum); System.out.println("奇数的和:"+evenSum); } }输出结果:
奇数的和:2500
奇数的和:2550 -
输出1到100之间能被5整除的数,并且每行输出3个
public class Test { public static void main(String[] args) { for (int i = 1, j = 0; i <= 100; i++) { if(i%5 == 0){ System.out.print(i+" ");//System.out.print()输出后不换行 j++; } if(j == 3){//每输出3个数便换行 j = 0; System.out.println();//System.out.printLn()输出后自动换行 } } } }输出结果:
5 10 15
20 25 30
35 40 45
50 55 60
65 70 75
80 85 90
95 100 -
打印九九乘法表
public class Test { public static void main(String[] args) { for (int i = 1; i <= 9; i++){ for (int j = 1; j <= i; j++){ System.out.print(j+" x "+i+" = "); System.out.printf("%-5d",j*i);//5代表输出值占5个字符,-代表左对齐 } System.out.println(); } } }输出结果:
1 x 1 = 1 1 x 2 = 2 2 x 2 = 4 1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 1 x 4 = 4 2 x 4 = 8 3 x 4 = 12 4 x 4 = 16 1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25 1 x 6 = 6 2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30 6 x 6 = 36 1 x 7 = 7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35 6 x 7 = 42 7 x 7 = 49 1 x 8 = 8 2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40 6 x 8 = 48 7 x 8 = 56 8 x 8 = 64 1 x 9 = 9 2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45 6 x 9 = 54 7 x 9 = 63 8 x 9 = 72 9 x 9 = 81 -
打印三角形
public class Test { public static void main(String[] args) { for (int i = 1; i <= 5; i++){ for (int j = 5; j >= i; j--){ System.out.print(" "); } for (int j = 1; j <= i; j++){ System.out.print("*"); } for (int j = 2; j <= i; j++){//j = 2是因为这个循环在第一行不输出* System.out.print("*"); } System.out.println(); } } }输出结果:
* *** ***** ******* *********public class Test { public static void main(String[] args) { for (int i = 1; i <= 5; i++){ for (int j = 1; j <= i; j++){ System.out.print(" "); } for (int j = 5; j >= i; j--){ System.out.print("*"); } for (int j = 4; j >= i; j--){//j = 2是因为这个循环在第一行不输出* System.out.print("*"); } System.out.println(); } } }输出结果:
********* ******* ***** *** *

浙公网安备 33010602011771号