Java SE14之後,switch支援運算式形式var quotient = score / 10;
var level = switch(quotient) {
case 10, 9 -> 'A';
case 8 -> 'B';
default -> 'D';
}
也可以改用yield指定switch的運算值var level = switch(quotient) {
case 10:
yield 'A';
}
或者混用var level = switch(quotient) {
case 10 -> 'A';
default -> {
String message = "error";
yield message;
}
}