Can we call a “case” inside another case in the same switch statement in Java?

耗尽温柔 提交于 2019-12-30 06:10:11

问题


My intention is to call two cases inside another case in the same switch statement,

switch (orderType) {
        case 1: 
            statement 1;
            break;
       case 2:
            statement 2;
            break;
       case 3:
             **call case 1;**
             **Call case 2;**
             break;
       default:
            break;`
}

Can we do that in Java?


回答1:


Although you cannot influence the switch cases directly, you can call switch's parent method from one case and pass different arguments. For example,

void foo(int param1, String param2, ...) {
    switch (param1) {
        case 0:
            foo(1, "some string");
            break;

        case 1:
            //do something
            break;

        default:
            break;
    }
}



回答2:


No, you can't jump to the code snippet in another switch case. You can however extract the code into an own method that can be called from another case:

switch (orderType) {
    case 1: 
        someMethod1();
        break;
    case 2:
        someMethod2();
        break;
    case 3:
        someMethod1();
        someMethod2();
        break;
    default:
        break;
}

void someMethod1() { ... }
void someMethod2() { ... }



回答3:


You can't just call a another case block like this. What you could do, though, is wrap each of these blocks in a method, and reuse them:

private void doSomething() {
    // implementation
}

private void doSomethingElse() {
    // implementation
}

private void runSwitch(int order) {

    switch (orderType) {
           case 1: 
                doSomething();
                break;
           case 2:
                doSomethingElse();
                break;
           case 3:
                doSomething();
                doSomethingElse();
                break;
           default:
                break;
    }
}



回答4:


As others have pointed out, you can implement your switch cases inside a method which takes a parameter, which corresponds to the desired switch case. These switch cases can recursively call the same method, with the desired switch cases. Here is a fully-working example which allows you to check if a year is a leap year, or not:

public class LeapYear {

    static int year = 2016;

    public static void main(String[] args) {

        leapSwitch(1);

    }

    public static void leapSwitch(int switchcase) {

        switch (switchcase) {

        case 1: {
            if (year % 4 == 0) {
                leapSwitch(2);
            } else {
                leapSwitch(5);
            }
        }
            break;

        case 2: {
            if (year % 100 == 0) {
                leapSwitch(3);

            } else {
                leapSwitch(4);
            }
        }
            break;

        case 3: {
            if (year % 400 == 0) {
                leapSwitch(4);

            } else {
                leapSwitch(5);
            }
        }
            break;

        case 4: {
            System.out.println(year+ " is a leap year!");
        }
            break;

        case 5: {
            System.out.println("Not a leap year...");
        }
            break;

        }
    }
}



回答5:


You can use this trick in some case :

switch (orderType) {
   case 3: 
        statement 3;
   case 2:
        statement 2;
   case 1:
        statement 1;
        break;
   default:
        break;`

}



来源:https://stackoverflow.com/questions/31498571/can-we-call-a-case-inside-another-case-in-the-same-switch-statement-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!