Java switch 敘述
switch 敘述用於根據變數的值執行不同的程式碼區塊,適合多重選擇的情況。
基本語法
switch (expression) {
case value1:
// 程式碼
break;
case value2:
// 程式碼
break;
default:
// 預設程式碼
}
基本範例
int day = 3;
switch (day) {
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期日");
break;
default:
System.out.println("無效的日期");
}
// 輸出:星期三
break 的重要性
沒有 break 會繼續執行下一個 case(fall-through):
int num = 2;
switch (num) {
case 1:
System.out.println("一");
case 2:
System.out.println("二");
case 3:
System.out.println("三");
default:
System.out.println("其他");
}
// 輸出:
// 二
// 三
// 其他
多個 case 共用程式碼
int month = 2;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println("31 天");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println("30 天");
break;
case 2:
System.out.println("28 或 29 天");
break;
default:
System.out.println("無效月份");
}
支援的資料型別
byte、short、int、charString(Java 7+)enum- 包裝類別(
Byte、Short、Integer、Character)
字串 switch
String fruit = "apple";
switch (fruit) {
case "apple":
System.out.println("蘋果");
break;
case "banana":
System.out.println("香蕉");
break;
case "orange":
System.out.println("橘子");
break;
default:
System.out.println("未知水果");
}
列舉 switch
enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
Day today = Day.WEDNESDAY;
switch (today) {
case MONDAY:
case TUESDAY:
case WEDNESDAY:
case THURSDAY:
case FRIDAY:
System.out.println("工作日");
break;
case SATURDAY:
case SUNDAY:
System.out.println("週末");
break;
}
Switch 表達式 (Java 12+)
更簡潔的寫法,使用 -> 箭頭:
int day = 3;
String dayName = switch (day) {
case 1 -> "星期一";
case 2 -> "星期二";
case 3 -> "星期三";
case 4 -> "星期四";
case 5 -> "星期五";
case 6 -> "星期六";
case 7 -> "星期日";
default -> "無效";
};
System.out.println(dayName); // 星期三
多個值
int month = 2;
int days = switch (month) {
case 1, 3, 5, 7, 8, 10, 12 -> 31;
case 4, 6, 9, 11 -> 30;
case 2 -> 28;
default -> 0;
};
yield 關鍵字
需要執行多行程式碼時使用 yield 回傳值:
int day = 3;
String result = switch (day) {
case 1, 2, 3, 4, 5 -> {
System.out.println("處理工作日...");
yield "工作日";
}
case 6, 7 -> {
System.out.println("處理週末...");
yield "週末";
}
default -> "無效";
};
更多詳細說明請參考 Java Switch 表達式。
switch vs if-else
| 情況 | 建議使用 |
|---|---|
| 單一變數的多值比較 | switch |
| 範圍判斷 | if-else |
| 複雜的條件組合 | if-else |
| 可讀性較重要 | switch |
// 適合 switch
switch (command) {
case "start" -> start();
case "stop" -> stop();
case "restart" -> restart();
default -> showHelp();
}
// 適合 if-else
if (score >= 90) {
// 範圍判斷
} else if (score >= 80) {
// ...
}
實際應用
選單系統
Scanner scanner = new Scanner(System.in);
System.out.println("1. 新增 2. 刪除 3. 修改 4. 離開");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("執行新增...");
break;
case 2:
System.out.println("執行刪除...");
break;
case 3:
System.out.println("執行修改...");
break;
case 4:
System.out.println("再見!");
break;
default:
System.out.println("無效選項");
}
計算器
char operator = '+';
int a = 10, b = 5;
int result = switch (operator) {
case '+' -> a + b;
case '-' -> a - b;
case '*' -> a * b;
case '/' -> a / b;
default -> throw new IllegalArgumentException("無效運算子");
};
System.out.println(result); // 15