【发布时间】:2021-12-26 07:02:26
【问题描述】:
我正在研究 switch 表达式,我想我发现了一个奇怪的行为:
public static boolean isTimeToParty(Day day) {
switch (day) {
case MONDAY -> dog(); //expression allowed
case TUESDAY -> 2 + 2; //error: not a statement
case WEDNESDAY -> true; //error: not a statement
default -> System.out.println("");
}
return false;
}
public static int dog() {
return 2;
}
为什么我可以键入值dog() 这是一个表达式,但不允许使用其他类型的表达式? Intellij 提示我 "not a statement" 错误。
提前致谢。
【问题讨论】:
标签: java switch-statement conditional-statements switch-expression