在Java中,使用条件语句和循环结构确定控制流程,在本文中,主要包括块作用域、条件语句、循环结构、中断循环这四部分。
一、块作用域
块,也叫复合语句,是指由一对大括号括起来的若干条Java语句。块决定了变量的作用域。一个块可以嵌套多个块。
二、条件语句
如果判断超过三层,建议拆分开来写,这样更加清晰。
1package javalearningday04; 2 3/** 4 * 条件语句 5 * @author 小川94 6 * @date 2018年1月31日 7 */ 8public class IfElseDemo { 9 10 public static void main(String[] args) { 11 testMethod(78); 12 } 13 14 /** 15 * 判断成绩 16 */ 17 public static void testMethod(int score) { 18 if (score < 0) { 19 System.out.println("请传入正确的分数!"); 20 } else { 21 if (score >= 90 && score <= 100) { 22 System.out.println("成绩优秀"); 23 } else if (score >= 80) { 24 System.out.println("成绩良好"); 25 } else if (score >= 70) { 26 System.out.println("成绩一般"); 27 } else if (score >= 60) { 28 System.out.println("成绩及格"); 29 } else { 30 System.out.println("不及格"); 31 } 32 } 33 } 34}
三、循环结构
3.1 for循环
1package javalearningday04; 2 3/** 4 * for循环的用法 5 * @author 小川94 6 * @date 2018年1月31日 7 */ 8public class ForDemo { 9 10 public static void main(String[] args) { 11 testMethod(); 12 testMethod2(); 13 } 14 15 /** 16 * 计算1,2,3...,100的和 17 */ 18 public static void testMethod() { 19 int sum = 0; 20 for (int i=1; i<101; i++) { 21 sum += i; 22 } 23 System.out.println(sum); //sum = 5050 24 } 25 26 public static void testMethod2() { 27 String[] strArray = {"小明","小马","小王"}; 28 // foreach循环遍历 29 for (String str : strArray) { 30 System.out.println(str); // 依次打印 小明 小马 小王 31 } 32 // 两种写法的输出结果都是一样的 33 for (int i=0; i<strArray.length; i++) { 34 System.out.println(strArray[i]); // 依次打印 小明 小马 小王 35 } 36 } 37 38}
3.2 do{...}while()循环
先执行,再判断。无论while中的条件是否成立,都会执行一次循环体。
1package javalearningday04; 2 3/** 4 * do{}while();的用法 5 * @author 小川94 6 * @date 2018年1月31日 7 */ 8public class DoWhileDemo { 9 10 public static void main(String[] args) { 11 testMethod(); 12 testMethod2(); 13 } 14 15 /** 16 * 计算1,2,3...,100的和 17 * 先执行,后判断 18 */ 19 public static void testMethod() { 20 int i = 1; 21 int sum = 0; 22 do{ 23 sum += i; 24 i++; 25 }while(i<101); 26 System.out.println(sum); // sum = 5050 27 System.out.println(i); // i = 101 28 } 29 30 /** 31 * do{}while();的循环结构,至少会执行一次循环体 32 * 在while中的条件不成立时,已经执行了一次循环体 33 */ 34 public static void testMethod2() { 35 int i = 1; 36 int sum = 0; 37 do{ 38 sum += i; 39 i++; 40 }while(i<1); 41 System.out.println(sum); // sum = 1 42 System.out.println(i); // i = 2 43 } 44}
3.3 while(){...}循环
先判断,再执行。只有条件成立,才会进入循环体。
1package javalearningday04; 2 3/** 4 * while(){}的用法 5 * @author 小川94 6 * @date 2018年1月31日 7 */ 8public class WhileDemo { 9 10 public static void main(String[] args) { 11 testMethod(); 12 testMethod2(); 13 } 14 15 /** 16 * 计算1,2,3...,100的和 17 * 先判断,后执行 18 */ 19 public static void testMethod() { 20 int i = 1; 21 int sum = 0; 22 while( i<101 ){ 23 sum += i; 24 i++; 25 } 26 System.out.println(sum); // sum = 5050 27 System.out.println(i); // i = 101 28 } 29 30 /** 31 * 只有while()中的条件成立时,才会执行循环体 32 * 如果while()中的条件永久为true,则会进入死循环,对程序会造成非常严重的后果, 33 * 开发中需要严格判断循环条件!避免出现死循环 34 */ 35 public static void testMethod2() { 36 int i = 1; 37 int sum = 0; 38 while( i<1 ){ 39 sum += i; 40 i++; 41 } 42 System.out.println(sum); // sum = 0 43 System.out.println(i); // i = 1 44 } 45 46}
3.4 独特的switch{}
1package javalearningday04; 2 3/** 4 * switch{}的用法 5 * @author 小川94 6 * @date 2018年1月31日 7 */ 8public class SwitchCaseDemo { 9 10 public static void main(String[] args) { 11 testMethodWithInt(3); 12 testMethodWithByte((byte)3); 13 testMethodWithChar((char)3); 14 testMethodWithShort((short)3); 15 testMethodWithString("MONDAY"); 16 testMethodWithEnum(SIZE.MEDIUM); 17 testSwitchWithoutBreak("MONDAY"); 18 } 19 20 // 支持int 21 public static void testMethodWithInt(int num) { 22 switch (num) { 23 case 0: 24 System.out.println("num等于0"); 25 break; 26 default: 27 System.out.println(num); 28 break; 29 } 30 } 31 32 // 支持byte 33 public static void testMethodWithByte(byte num) { 34 switch (num) { 35 case 0: 36 System.out.println("num等于0"); 37 break; 38 default: 39 System.out.println(num); 40 break; 41 } 42 } 43 44 // 支持char 45 public static void testMethodWithChar(char num) { 46 switch (num) { 47 case 0: 48 System.out.println("num等于0"); 49 break; 50 default: 51 System.out.println(num); 52 break; 53 } 54 } 55 56 // 支持short 57 public static void testMethodWithShort(short num) { 58 switch (num) { 59 case 0: 60 System.out.println("num等于0"); 61 break; 62 default: 63 System.out.println(num); 64 break; 65 } 66 } 67 68 // 支持字符串 69 public static void testMethodWithString(String str) { 70 switch (str) { 71 case "MONDAY": 72 System.out.println("是星期一"); 73 break; 74 default: 75 System.out.println(str); 76 break; 77 } 78 } 79 80 public enum SIZE{ 81 // 小号 82 SMALL, 83 // 中号 84 MEDIUM, 85 // 大号 86 LARGE; 87 } 88 89 public SIZE size; 90 91 public SwitchCaseDemo(SIZE size) { 92 this.size = size; 93 } 94 // 支持枚举类型 95 public static void testMethodWithEnum(SIZE size) { 96 switch (size) { 97 case SMALL: 98 System.out.println("是小号"); 99 break; 100 case MEDIUM: 101 System.out.println("是中号"); 102 break; 103 case LARGE: 104 System.out.println("是大号"); 105 break; 106 default: 107 System.out.println("没有其他号了"); 108 break; 109 } 110 } 111 112 // 不写break语句,则每种情况都会执行 113 public static void testSwitchWithoutBreak(String str) { 114 switch (str) { 115 case "MONDAY": 116 System.out.println("吃包子"); 117 case "SUNDAY": 118 System.out.println("吃面条"); 119 default: 120 System.out.println("喝粥"); 121 } 122 } 123 124 // 不支持long类型的数据 125 /*public static void testMethodWithLong(long num) { 126 switch (num) { 127 case 0: 128 System.out.println("num等于0"); 129 break; 130 default: 131 System.out.println(num); 132 break; 133 } 134 }*/ 135 136 // 不支持double类型的数据 137 /*public static void testMethodWithDouble(double num) { 138 switch (num) { 139 case 0: 140 System.out.println("num等于0"); 141 break; 142 default: 143 System.out.println(num); 144 break; 145 } 146 }*/ 147}
四、中断循环
中断循环需要用到两个关键字,一是continue,另一个是break。
continue是指将其后面的执行语句跳过,进入下一次新的循环,整个循环结构是还在运行的,没有终止。
break是指结束掉整个循环结构,开始执行整个循环结构的后面的代码。
1package javalearningday04; 2 3/** 4 * continue、break的用法 5 * @author 小川94 6 * @date 2018年1月31日 7 */ 8public class ContinueBreakDemo { 9 10 public static void main(String[] args) { 11 testContinue(); 12 testBreak(); 13 } 14 15 /** 16 * 求数组中正数的和 17 * continue:在执行完continue语句后,其后的代码都不再执行, 18 * 结束本次循环,进入下一次循环,整个循环结构还在继续执行 19 */ 20 public static void testContinue() { 21 int[] arr = {1,2,3,4,-5,6}; 22 int sum = 0; 23 for(int i=0; i<arr.length; i++){ 24 if (arr[i]<0) { //过滤数组中的负数 25 continue; 26 } else { 27 sum += arr[i]; 28 } 29 } 30 // 跳过-5,计算1+2+3+4+6的和 31 System.out.println(sum); // sum = 16 32 } 33 34 /** 35 * break:在执行完break语句后,其后的代码都不再执行, 36 * 结束整个循环结构, 37 */ 38 public static void testBreak() { 39 int[] arr = {1,2,3,4,-5,6}; 40 int sum = 0; 41 for(int i=0; i<arr.length; i++){ 42 if (arr[i]<0) { 43 break; // 与上面的代码一样,只是将continue换成了break 44 } else { 45 sum += arr[i]; 46 } 47 } 48 // 只会计算1+2+3+4的和 49 System.out.println(sum); // sum = 10 50 } 51}
上面的代码都上传至了GitHub,地址是https://github.com/XiaoChuan94/javalearning/tree/master/javalearningday04,有需要的可以去下载观看,如果喜欢就给个star吧!如有不足,欢迎下方留言交流。
文章首发于我的个人公众号:悦乐书**。喜欢分享一路上听过的歌,看过的电影,读过的书,敲过的代码,深夜的沉思。期待你的关注!**

公众号后台输入关键字“Java学习电子书”,即可获得12本Java学习相关的电子书资源,如果经济能力允许,还请支持图书作者的纸质正版书籍,创作不易。