java 在 switch-case 中切换到 default case

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42276754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:30:21  来源:igfitidea点击:

Switch to default case in switch-case

javaswitch-statementdefault

提问by Fabb111

Is it possible to go to the default case while I'm in case 1/2/etc.?

在我遇到 1/2/等情况时,是否可以使用默认情况?

switch(num) {
    case 1:
        if(foo) {
            //do something
        } else {
            //go to default
        }

        ...
        ...
        ...

    default:
        //do something
}

回答by m.antkowicz

Even if you change numvalue when you are in switchthe value for comparision is the value of variable beforestart of switch so if you've entered one of casebranches you cannot enter again into default.

即使您numswitch比较值中更改值也是 switch 开始之前变量的值,因此如果您已进入case分支之一,则无法再次进入default.

However you can do something like

但是你可以做类似的事情

boolean doDefault = false;
switch(num) {
    case 1:
        if(foo) {
            ...
        }
        else {
            doDefault = true;
        }
        break;
}

if(doDefault) {
    //do something
}

回答by Barthy

You could write the logic that is supposed to happen in the defaultblock in a function and call it when necessary:

您可以default在函数的块中编写应该发生的逻辑,并在必要时调用它:

void defAction(){
    printf("Hello, World!\n");
}

int main() {

    int num = 1, foo = 0;

    switch(num) {
        case 1:
            if(foo) {
                printf("Goodbye, World!\n");
            } else {
                defAction();
            }
            break;
        default:
            defAction();
    }

    return 0;
}

Remember, that if a case does not use break;, all cases below it (including default) are also called. This way, if you only break out of the switch if(foo), it will proceed to the default. This is only really useful, if you want to jump to defaultfrom no more than one case, in which case you put that caselast, right before default.

请记住,如果一个案例不使用break;,则它下面的所有案例(包括default)也会被调用。这样,如果你只跳出 switch if(foo),它就会进入default. 这真的很有用,如果你想default从不超过一个跳转到case,在这种情况下,你把它放在case最后,就在 之前default

use with caution / see comments below

谨慎使用/见下面的评论

switch(num) {
    case 2:
        // do sth
        break;
    case 3:
        // do sth
        break;
    case 4:
        // do sth
        break;
    case 1:
        if(foo) {
            printf("Goodbye, World!\n");
            break;
        }
        // ELSE don't break, use default
    default:
        defAction();
}

回答by OneCricketeer

It's possible to make a method

可以制作一个方法

switch(num) {
    case 1:
        if(foo) {
            //do something
        } else {
            foo();
        }
    default:
        foo();
    }

回答by Kiran Kumar

You achieve this in a way below.

您可以通过以下方式实现这一点。

Suppose you need to go to the default case after case 1,2,8 only then you can write below code.

假设您需要在案例 1,2,8 之后转到默认案例,然后您可以编写以下代码。

 switch(Choice){
    case 1 : //do something
    case 2 : //do something
    case 8 : //do something
    default: //to do default operation   
    break;
    case 3 : 
    case 4 : 
  }

回答by Kiran Kumar

Not possible,doesn't make sense in real time application or in any use case,but if you want to go in default than you can do this by any of the following way-

不可能,在实时应用程序或任何用例中都没有意义,但是如果您想使用默认值,则可以通过以下任何一种方式来执行此操作-

  1. Remove all break than you will meat default at end.
  2. Call goDefault() method with value not matches with any case.
  1. 删除所有中断,而不是最后的肉默认值。
  2. 调用 goDefault() 方法,其值与 any 不匹配case

回答by CornePlas

No you can't, but you can call the same method or leave out the breaks.

不,你不能,但你可以调用相同的方法或省略休息时间。

回答by Dmytro Grynets

You can put them before default and don't brake at the end of case, then it will execute all statements before break, no matter what is the case

你可以把它们放在 default 之前,不要在 case 结束时刹车,那么它会在 break 之前执行所有语句,不管是什么情况