Java中的Switch Case语句的奇怪行为[重复]

时间:2021-06-26 10:21:49

This question already has an answer here:

这个问题在这里已有答案:

I understand that Java switch case are designed this way but why is this behavior in Java

我知道Java switch case是这样设计的,但为什么这种行为是用Java编写的

    int x = 1;

    switch(x){
    case 1: System.out.println(1);
    case 2: System.out.println(2);
    case 3: System.out.println(3);
    default: System.out.println("default");

    }

output : 1
         2   
         3
         default

My question is why case 2 and 3 are executed? I know I omitted break statement
but x was never 2 or 3 but case 2 and case 3 still executes?

我的问题是为什么案例2和3被执行?我知道我省略了break语句,但x从不是2或3但是案例2和案例3仍然执行?

4 个解决方案

#1


3  

I know I omitted break statement but x was never 2 or 3 but case 2 and case 3 still executes?

我知道我省略了break语句,但x从不是2或3但是案例2和案例3仍然执行?

Straight from the doc :

直接来自doc:

The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.

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

#2


4  

There is no break statement so all case are executed

没有break语句所以所有情况都会被执行

Use break statements

使用break语句

switch(x){
    case 1: System.out.println(1);break;
    case 2: System.out.println(2);break;
    case 3: System.out.println(3);break;
    default: System.out.println("default");

    }

#3


1  

You need to add break statement for each case. As there is no break statement all cases are getting executed.

您需要为每个案例添加break语句。由于没有中断语句,所有案例都会被执行。

#4


0  

You are missing the Break statement.

你错过了Break语句。

switch(x){
case 1: System.out.println(1);
break;
case 2: System.out.println(2);
break;
case 3: System.out.println(3);
break;
default: System.out.println("default");
}

Check the The switch Statement

检查switch语句

The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.

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

#1


3  

I know I omitted break statement but x was never 2 or 3 but case 2 and case 3 still executes?

我知道我省略了break语句,但x从不是2或3但是案例2和案例3仍然执行?

Straight from the doc :

直接来自doc:

The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.

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

#2


4  

There is no break statement so all case are executed

没有break语句所以所有情况都会被执行

Use break statements

使用break语句

switch(x){
    case 1: System.out.println(1);break;
    case 2: System.out.println(2);break;
    case 3: System.out.println(3);break;
    default: System.out.println("default");

    }

#3


1  

You need to add break statement for each case. As there is no break statement all cases are getting executed.

您需要为每个案例添加break语句。由于没有中断语句,所有案例都会被执行。

#4


0  

You are missing the Break statement.

你错过了Break语句。

switch(x){
case 1: System.out.println(1);
break;
case 2: System.out.println(2);
break;
case 3: System.out.println(3);
break;
default: System.out.println("default");
}

Check the The switch Statement

检查switch语句

The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.

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