简体   繁体   English

Java:我可以在 switch 语句中只遇到一种情况吗

[英]Java: Can I fall through only one case in a switch statement

In Java, can I fall through only one of the cases in a switch statement?在 Java 中,我可以只通过switch语句中的一种情况吗? I understand that if I break , I will fall through to the end of the switch statement.我明白,如果我break ,我将失败到switch语句的末尾。

Here's what I mean.这就是我的意思。 Given the following code, on case 2, I want to execute case 2 and case 1. On case 3, I want to execute case 3 and case 1, but not case 2.给定以下代码,在案例 2 上,我想执行案例 2 和案例 1。在案例 3 上,我想执行案例 3 和案例 1,但不执行案例 2。

switch(option) {
    case 3:  // code
             // skip the next case, not break
    case 2:  // code
    case 1:  // code
}

No, what you are after is not possible with a switch statement.不,使用switch语句是不可能的。 You will fall through each case until you hit a break .您将通过每个case直到您遇到break Perhaps you want case 1 to be outside of your switch statement, so that it is executed regardless.也许您希望case 1在您的switch语句之外,以便它无论如何都会被执行。

Put the code into methods and call as appropriate.将代码放入方法中并适当调用。 Following your example:按照你的例子:

void case1() {
    // Whatever case 1 does
}

void case2() {
    // Whatever case 2 does
}

void case3() {
    // Whatever case 3 does
}

switch(option) {
    case 3:
        case3();
        case1();
        break;
    case 2:
        case2();
        case1();
        break;
    case 1: 
        case1();   // You didn't specify what to do for case 1, so I assume you want case1()
        break;
    default:
        // Always a good idea to have a default, just in case demons are summoned
}

Of course case3() , case2() ... are very poor method names, you should rename to something more meaningful about what the method actually does.当然, case3()case2() ... 是非常糟糕的方法名称,您应该将其重命名为对方法实际执行的操作更有意义的名称。

My suggestion is to not use fallthrough for anything except cases like the following:我的建议是,除了以下情况外,不要对任何事情使用 fallthrough:

switch (option) {
    case 3:
        doSomething();
        break;
    case 2:
    case 1:
        doSomeOtherThing();
        break;
    case 0:
        // do nothing
        break;
}

That is, giving several cases the exact same block of code to handle them (by "stacking" the case labels), making it more or less obvious what the flow is here.也就是说,为几个案例提供完全相同的代码块来处理它们(通过“堆叠” case标签),使这里的流程或多或少变得显而易见。 I doubt most programmers intuitively check for case fall through (because the indentation makes a case look like as a proper block) or can efficiently read code that relies on it - I know I don't.我怀疑大多数程序员会凭直觉检查 case fall through(因为缩进使 case 看起来像一个适当的块)或者可以有效地读取依赖它的代码 - 我知道我不知道。

switch(option) 
{
    case 3:
        ...
        break;
    case 2: 
        ...
        break;
}

... // code for case 1

You can custom the condition if you want to split cases如果要拆分案例,可以自定义条件

const { type, data } = valueNotifications

    let convertType = ''
    if (data?.type === 'LIVESTREAM' && type === 'NORMAL') { 
      convertType = 'LIVESTREAM1' 
    } else convertType = type 

    switch (convertType) 

My use case has split the type from value notifications, but I have a specific LiveStream case which only shows in data.type is 'LIVESTREAM'我的用例已将类型与值通知分开,但我有一个特定的 LiveStream 案例,它只显示在 data.type 中是“LIVESTREAM”

Something like this maybe.也许像这样的事情。

switch(option) {
    case 3:  // code
             // skip the next case, not break
        // BLOCK-3
    case 2:  // code
        if(option == 3) break;
        // BLOCK-2
    case 1:  // code
        // BLOCK-1
}

In switch statement if you don't break the subsequent case is executed.在 switch 语句中,如果不break ,则执行后续 case。 To give you simple example给你简单的例子

    int value = 2;
    switch(value) {
    case 1: 
        System.out.println("one");
        break;
    case 2: 
        System.out.println("two");
    case 3: 
        System.out.println("three");
        break;
    }

Will output会输出

two
three

Because break wansn't executed on case 2因为break不会在 case 2 上执行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM