Java开关-缺少中断后执行的案例不正确。为什么?
我在网上学习Java,来到了关于交换机的课程。为了消磨时间,我在一个将要执行的特定案例之后删除了一个中断。我原以为它会检查下一个案例,查看条件是否为false,然后在退出之前跳到默认案例。相反,它执行了错误的案例,并在默认案例之前中断 这里出了什么问题Java开关-缺少中断后执行的案例不正确。为什么?,java,switch-statement,Java,Switch Statement,我在网上学习Java,来到了关于交换机的课程。为了消磨时间,我在一个将要执行的特定案例之后删除了一个中断。我原以为它会检查下一个案例,查看条件是否为false,然后在退出之前跳到默认案例。相反,它执行了错误的案例,并在默认案例之前中断 这里出了什么问题 public class Switch { public static void main(String[] args) { char penaltyKick = 'R'; switch (penalt
public class Switch {
public static void main(String[] args) {
char penaltyKick = 'R';
switch (penaltyKick) {
case 'L': System.out.println("Messi shoots to the left and scores!");
break;
case 'R': System.out.println("Messi shoots to the right and misses the goal!");
case 'C': System.out.println("Messi shoots down the center, but the keeper blocks it!");
break;
default:
System.out.println("Messi is in position...");
}
}
}
编辑:忘了提了。结果是:
Messi shoots to the right and misses the goal!
Messi shoots down the center, but the keeper blocks it!
如果缺少中断,则执行将进入下一个案例,而无需再次检查条件。见:
break语句是必需的,因为没有它们,语句
在开关块中,fall-through:匹配大小写后的所有语句
标签是按顺序执行的,而不考虑
后续案例标签,直到遇到break语句
如果缺少中断,则执行将进入下一个案例,而无需再次检查条件。见:
break语句是必需的,因为没有它们,语句
在开关块中,fall-through:匹配大小写后的所有语句
标签是按顺序执行的,而不考虑
后续案例标签,直到遇到break语句
这就是它应该做的。没什么不对的,这就是它应该做的。没什么不对的。