Java开关 - 错过中断后执行的错误案例.为什么?

use*_*379 1 java switch-statement

我正在网上学习Java并且在切换课程中上课.为了愚弄,我会在特定情况(将被执行)之后删除其中一个中断.我曾想过它会检查下一个案例,看看条件是否为false,并在退出前跳到默认情况.相反,它执行了不正确的情况并在默认情况之前中断.

这里出了什么问题?

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...");

        }

    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:忘了提.输出是:

Messi shoots to the right and misses the goal! 
Messi shoots down the center, but the keeper blocks it!
Run Code Online (Sandbox Code Playgroud)

Vla*_*lad 5

如果您丢失了break,执行将在case不检查条件的情况下进入下一个.看到这里:

break语句是必需的,因为没有它们,switch块中的语句都会失败:匹配的case标签之后的所有语句都按顺序执行,而不管后续case标签的表达式,直到遇到break语句.