Java入門(五):控制流程程,java入門控制流程程
在Java中,使用條件陳述式和迴圈結構確定控制流程程,在本文中,主要包括塊範圍、條件陳述式、迴圈結構、中斷迴圈這四部分。
一、塊範圍
塊,也叫複合陳述式,是指由一對大括弧括起來的若干條Java語句。塊決定了變數的範圍。一個塊可以嵌套多個塊。
二、條件陳述式
如果判斷超過三層,建議拆分開來寫,這樣更加清晰。
package javalearningday04;/** * 條件陳述式 * @author 小川94 * @date 2018年1月31日 */public class IfElseDemo { public static void main(String[] args) { testMethod(78); } /** * 判斷成績 */ public static void testMethod(int score) { if (score < 0) { System.out.println("請傳入正確的分數!"); } else { if (score >= 90 && score <= 100) { System.out.println("成績優秀"); } else if (score >= 80) { System.out.println("成績良好"); } else if (score >= 70) { System.out.println("成績一般"); } else if (score >= 60) { System.out.println("成績及格"); } else { System.out.println("不及格"); } } }}
三、迴圈結構
3.1 for迴圈
package javalearningday04;/** * for迴圈的用法 * @author 小川94 * @date 2018年1月31日 */public class ForDemo { public static void main(String[] args) { testMethod(); testMethod2(); } /** * 計算1,2,3...,100的和 */ public static void testMethod() { int sum = 0; for (int i=1; i<101; i++) { sum += i; } System.out.println(sum); //sum = 5050 } public static void testMethod2() { String[] strArray = {"小明","小馬","小王"}; // foreach迴圈遍曆 for (String str : strArray) { System.out.println(str); // 依次列印 小明 小馬 小王 } // 兩種寫法的輸出結果都是一樣的 for (int i=0; i<strArray.length; i++) { System.out.println(strArray[i]); // 依次列印 小明 小馬 小王 } } }
3.2 do{...}while()迴圈
先執行,再判斷。無論while中的條件是否成立,都會執行一次迴圈體。
package javalearningday04;/** * do{}while();的用法 * @author 小川94 * @date 2018年1月31日 */public class DoWhileDemo { public static void main(String[] args) { testMethod(); testMethod2(); } /** * 計算1,2,3...,100的和 * 先執行,後判斷 */ public static void testMethod() { int i = 1; int sum = 0; do{ sum += i; i++; }while(i<101); System.out.println(sum); // sum = 5050 System.out.println(i); // i = 101 } /** * do{}while();的迴圈結構,至少會執行一次迴圈體 * 在while中的條件不成立時,已經執行了一次迴圈體 */ public static void testMethod2() { int i = 1; int sum = 0; do{ sum += i; i++; }while(i<1); System.out.println(sum); // sum = 1 System.out.println(i); // i = 2 }}
3.3 while(){...}迴圈
先判斷,再執行。只有條件成立,才會進入迴圈體。
package javalearningday04;/** * while(){}的用法 * @author 小川94 * @date 2018年1月31日 */public class WhileDemo { public static void main(String[] args) { testMethod(); testMethod2(); } /** * 計算1,2,3...,100的和 * 先判斷,後執行 */ public static void testMethod() { int i = 1; int sum = 0; while( i<101 ){ sum += i; i++; } System.out.println(sum); // sum = 5050 System.out.println(i); // i = 101 } /** * 只有while()中的條件成立時,才會執行迴圈體 * 如果while()中的條件永久為true,則會進入死迴圈,對程式會造成非常嚴重的後果, * 開發中需要嚴格判斷迴圈條件!避免出現死迴圈 */ public static void testMethod2() { int i = 1; int sum = 0; while( i<1 ){ sum += i; i++; } System.out.println(sum); // sum = 0 System.out.println(i); // i = 1 } }
3.4 獨特的switch{}
package javalearningday04;/** * switch{}的用法 * @author 小川94 * @date 2018年1月31日 */public class SwitchCaseDemo { public static void main(String[] args) { testMethodWithInt(3); testMethodWithByte((byte)3); testMethodWithChar((char)3); testMethodWithShort((short)3); testMethodWithString("MONDAY"); testMethodWithEnum(SIZE.MEDIUM); testSwitchWithoutBreak("MONDAY"); } // 支援int public static void testMethodWithInt(int num) { switch (num) { case 0: System.out.println("num等於0"); break; default: System.out.println(num); break; } } // 支援byte public static void testMethodWithByte(byte num) { switch (num) { case 0: System.out.println("num等於0"); break; default: System.out.println(num); break; } } // 支援char public static void testMethodWithChar(char num) { switch (num) { case 0: System.out.println("num等於0"); break; default: System.out.println(num); break; } } // 支援short public static void testMethodWithShort(short num) { switch (num) { case 0: System.out.println("num等於0"); break; default: System.out.println(num); break; } } // 支援字串 public static void testMethodWithString(String str) { switch (str) { case "MONDAY": System.out.println("是星期一"); break; default: System.out.println(str); break; } } public enum SIZE{ // 小號 SMALL, // 中號 MEDIUM, // 大號 LARGE; } public SIZE size; public SwitchCaseDemo(SIZE size) { this.size = size; } // 支援枚舉類型 public static void testMethodWithEnum(SIZE size) { switch (size) { case SMALL: System.out.println("是小號"); break; case MEDIUM: System.out.println("是中號"); break; case LARGE: System.out.println("是大號"); break; default: System.out.println("沒有其他號了"); break; } } // 不寫break語句,則每種情況都會執行 public static void testSwitchWithoutBreak(String str) { switch (str) { case "MONDAY": System.out.println("吃包子"); case "SUNDAY": System.out.println("吃麵條"); default: System.out.println("喝粥"); } } // 不支援long類型的資料 /*public static void testMethodWithLong(long num) { switch (num) { case 0: System.out.println("num等於0"); break; default: System.out.println(num); break; } }*/ // 不支援double類型的資料 /*public static void testMethodWithDouble(double num) { switch (num) { case 0: System.out.println("num等於0"); break; default: System.out.println(num); break; } }*/}
四、中斷迴圈
中斷迴圈需要用到兩個關鍵字,一是continue,另一個是break。
continue是指將其後面的執行語句跳過,進入下一次新的迴圈,整個迴圈結構是還在啟動並執行,沒有終止。
break是指結束掉整個迴圈結構,開始執行整個迴圈結構的後面的代碼。
package javalearningday04;/** * continue、break的用法 * @author 小川94 * @date 2018年1月31日 */public class ContinueBreakDemo { public static void main(String[] args) { testContinue(); testBreak(); } /** * 求數組中正數的和 * continue:在執行完continue語句後,其後的代碼都不再執行, * 結束本次迴圈,進入下一次迴圈,整個迴圈結構還在繼續執行 */ public static void testContinue() { int[] arr = {1,2,3,4,-5,6}; int sum = 0; for(int i=0; i<arr.length; i++){ if (arr[i]<0) { //過濾數組中的負數 continue; } else { sum += arr[i]; } } // 跳過-5,計算1+2+3+4+6的和 System.out.println(sum); // sum = 16 } /** * break:在執行完break語句後,其後的代碼都不再執行, * 結束整個迴圈結構, */ public static void testBreak() { int[] arr = {1,2,3,4,-5,6}; int sum = 0; for(int i=0; i<arr.length; i++){ if (arr[i]<0) { break; // 與上面的代碼一樣,只是將continue換成了break } else { sum += arr[i]; } } // 只會計算1+2+3+4的和 System.out.println(sum); // sum = 10 }}
上面的代碼都上傳至了GitHub,地址是https://github.com/XiaoChuan94/javalearning/tree/master/javalearningday04,有需要的可以去下載觀看,如果喜歡就給個star吧!如有不足,歡迎下方留言交流。
文章首發於我的個人公眾號:悅樂書。喜歡分享一路上聽過的歌,看過的電影,讀過的書,敲過的代碼,深夜的沉思。期待你的關注!
公眾號後台輸入關鍵字“Java學習電子書”,即可獲得12本Java學習相關的電子書資源,如果經濟能力允許,還請支援圖書作者的紙質正版書籍,創作不易。