无法在 Java 中获取 Switch 案例的行为

2022-01-19 00:00:00 switch-statement java

我用java 6写过小代码

公共类TestSwitch{公共静态无效主(字符串...参数){整数a = 1;System.out.println(开始");开关(一){情况1:{System.out.println(1);案例3:System.out.println(3);案例4:System.out.println(4);}案例2:{System.out.println(2);案例5:System.out.println(5);案例7:System.out.println(7);}}System.out.println(结束");}}

<块引用>

输出: start 1 2 end

我的编辑器正在显示案例 3"和案例 5"的孤立案例.它仍在运行并显示输出.

  • Java 中是否有类似概念的 Nastated 案例?

  • 为什么它会给出上述输出?相反,我认为这将是开始 1 结束"

    您的回复将不胜感激!

解决方案

switch 替换 if else 的 but switch 语法!= if else 语法.

你忘了在每个 case 后面加上 break.

所以下的条件不成立.

示例:

案例0:mColor.setText("#000000");休息;

您可以在文档中找到它

<块引用>

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

public static void main(String...args){整数a = 1;System.out.println("开始");开关(一){情况1:System.out.println(1);休息;案例2:System.out.println(2);休息;案例3:System.out.println(3);休息;案例4:System.out.println(4);休息;案例5:System.out.println(5);休息;案例7:System.out.println(7);休息;默认:System.out.println("什么都没有");}

I have written small code in java 6

public class TestSwitch{

public static void main(String... args){
    int a = 1;
    System.out.println("start");
    switch(a){
        case 1:{
            System.out.println(1);
            case 3:
                System.out.println(3);
            case 4:
                System.out.println(4);
        }
        case 2:{
            System.out.println(2);
            case 5:
                System.out.println(5);
            case 7:
                System.out.println(7);
        }
            
    }
    System.out.println("end");
}
}

Output: start 1 2 end

My editor is showing orphaned case for 'case 3' and 'case 5'.Still it is running and showing output.

  • Does Nastated cases like concept is there in Java?

  • And Why it is giving above output? rather i thought it will be 'start 1 end'

    Your response will be greatly appreciated!!

解决方案

Switch replaces if else's but switch syntax != If else syntax.

You forgot to put break after each case.

So conditions under fall through.

Example:

case 0:
          mColor.setText("#000000");
          break;

You can find that in docs

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.

public static void main(String... args){
        int a = 1;
        System.out.println("start");
        switch(a){
            case 1: 
                System.out.println(1);
                break;
            case 2:
                  System.out.println(2);
                  break;
            case 3:
                System.out.println(3);
                break;
            case 4:
                System.out.println(4);
                break;
            case 5:
                System.out.println(5);
                break;
            case 7:
                System.out.println(7);
                break;
            default:
                System.out.println("nothing");

            }

相关文章