1

我在 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");
}
}

输出:开始 1 2 结束

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

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

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

    您的回复将不胜感激!!

4

8 回答 8

2
switch(a){
    case 1:{
        System.out.println(1);
        case 3:

You cannot nest cases like this. Switch should look either like :

    switch(a){
    case 1:
        System.out.println(1);
        break;
    case 3:
       ....

or like this :

switch(a){
    case 1:
        System.out.println(1);
        switch(a) {
            case 3:
                //...
                break;
            case 5 :
                //...

And if you don't add break at the end of a case, the execution will continue after. You should add a break at the end of each cases if they should be executed separately.

于 2013-10-25T05:50:00.750 回答
2

Switch 替换 if else 的但 switch 语法!= If else 语法。

你忘了把break每个案例都放在后面。

因此,条件下降。

例子:

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

你可以在文档中找到

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

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

            }
于 2013-10-25T05:46:06.800 回答
1

You have not added break statement before case 2.

Refer this to know more about fall through.

Each break statement terminates the enclosing switch statement. Control flow continues
with the first statement following the switch block. 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.
于 2013-10-25T05:53:34.923 回答
1

您在案例 2 之前有错误的右大括号。案例 3,4 被解释为标签而不是案例。

于 2013-10-25T05:47:37.150 回答
1

您的代码将给出编译错误,因为我们不能在 case 之后使用花括号:确切的代码是:

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

并且输出将是 start 1 3 4 2 5 7 end 因为你没有在每个案例之后使用“break”。

于 2013-10-25T05:49:28.840 回答
1

由于他们在执行中的无中断语句case 1:直接跳转到case 2:并最终打印“start 1 2 end”..

于 2013-10-25T05:49:36.463 回答
1
int a = 1;
System.out.println("start");
switch (a) {
case 1: {
    System.out.println(1);
    break;
}
case 3: {
    System.out.println(3);
    break;
}
case 4: {
    System.out.println(4);
    break;
}
case 2: {
    System.out.println(2);
    break;
}
case 5: {
    System.out.println(5);
    //no break will fall through and print 7 too 
}
case 7: {
    System.out.println(7);
    break;
}
default:{
    System.out.println("none");
}

}
于 2013-10-25T05:53:59.010 回答
0
 See if a=1 then your case 1 will work then 1 will pe printed if as we have not using      break after case 1 so all cases are working in flow so output is coming like this if you want to execute only one case at one time then you have to put break after one case like

switch(a){
        case 1:
            System.out.println(1);
             break;
            case 3:
                System.out.println(3);
             break;
            case 4:
                System.out.println(4);
             break;

然后它会在遇到 break 语句时跳出 switch case

于 2013-10-25T08:22:38.050 回答