
2. switch后面小括號當中只能是下列資料型別:
基本資料型別:byte/short/char/int
參考資料型別:String字串、enum列舉
/* switch陳述句使用的注意事項: 1. 多個case后面的數值不可以重復, 2. switch后面小括號當中只能是下列資料型別: 基本資料型別:byte/short/char/int 參考資料型別:String字串、enum列舉 3. switch陳述句格式可以很靈活:前后順序可以顛倒,而且break陳述句還可以省略, “匹配哪一個case就從哪一個位置向下執行,直到遇到了break或者整體結束為止,” */ public class Demo08SwitchNotice { public static void main(String[] args) { int num = 2; switch (num) { case 1: System.out.println("你好"); break; case 2: System.out.println("我好"); // break; case 3: System.out.println("大家好"); break; default: System.out.println("他好,我也好,"); break; } // switch } }View Code
for、while、do-while回圈的區別:
1. 如果條件判斷從來沒有滿足過,那么for回圈和while回圈將會執行0次,但是do-while回圈會執行至少一次,
2. for回圈的變數在小括號當中定義,只有回圈內部才可以使用,while回圈和do-while回圈初始化陳述句本來就在外面,所以出來回圈之后還可以繼續使用,
/* 三種回圈的區別, 1. 如果條件判斷從來沒有滿足過,那么for回圈和while回圈將會執行0次,但是do-while回圈會執行至少一次, 2. for回圈的變數在小括號當中定義,只有回圈內部才可以使用,while回圈和do-while回圈初始化陳述句本來就在外面,所以出來回圈之后還可以繼續使用, */ public class Demo13LoopDifference { public static void main(String[] args) { for (int i = 1; i < 0; i++) { System.out.println("Hello"); } // System.out.println(i); // 這一行是錯誤寫法!因為變數i定義在for回圈小括號內,只有for回圈自己才能用, System.out.println("================"); int i = 1; do { System.out.println("World"); i++; } while (i < 0); // 現在已經超出了do-while回圈的范圍,我們仍然可以使用變數i System.out.println(i); // 2 } }View Code
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/241210.html
標籤:Java
