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

后端 未结 5 1555
傲寒
傲寒 2021-01-04 10:28

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

switch (orderType) {
        case 1: 
            statement 1;
                   


        
相关标签:
5条回答
  • 2021-01-04 10:44

    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() { ... }
    
    0 讨论(0)
  • 2021-01-04 10:48

    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;
        }
    }
    
    0 讨论(0)
  • Using methods is the best way to do it as mentioned in the accepted answer but for some reasons if you are unable to create method, Here is one more way to do this without using methods

    boolean isBreakTheLoop = false, isCase2Required = false;
    while(!isBreakTheLoop){
        switch (orderType) {
           case 1: 
                statement 1;
                if(isCase2Required){
                   orderType = 2;
                   break;
                }
                isBreakTheLoop = true;
                break;
           case 2:
                statement 2;
                isBreakTheLoop = true;
                break;
           case 3:
                orderType = 1;
                isCase2Required = true;
                break;
           default:
                isBreakTheLoop = true;
                break;
        }
    }
    
    0 讨论(0)
  • 2021-01-04 11:05

    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;`
    

    }

    0 讨论(0)
  • 2021-01-04 11:08

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题