String day = "Mon";
String fullDayName = "";
switch(day){
case "Mon":
fullDayName = "Monday";
break;
case "Tue" :
fullDayName = "Tuesday";
break;
default:
fullDayName = "Nah";
}
String day = "Mon";
String fullDayName = "";
switch(day){
case "Mon" -> fullDayName = "Monday";
case "Tue" -> fullDayName = "Tuesday";
default -> fullDayName = "Nah";
}
也可以直接回傳結果給指定的參數
String day = "Mon";
String fullDayName = "";
fullDayName = switch(day){
case "Mon" -> "Monday";
case "Tue" -> "Tuesday";
default -> "Nah";
}; // 注意,因為此時是expression,因此最後要加上分號
也可以使用yield關鍵字的寫法 (不想使用arrow表示式的話,應該是沒這麼堅持吧哈哈)
String day = "Mon";
String fullDayName = "";
fullDayName = switch(day){
case "Mon" : yield "Monday";
case "Tue" : yield "Tuesday";
default : yield "Nah";
}; // 注意,因為此時是expression,因此最後要加上分號