Java Switch Statement

后端 未结 8 2226
南笙
南笙 2021-02-07 13:48

I have a problem using switch statement when I tried to deal with a special situation. For example, I have 3 cases: A, B, C.

  • for A, I want to do statement_1 and st
8条回答
  •  后悔当初
    2021-02-07 14:25

    Why not nest the switch into the if statement? there is no-repeat code this way.

    if(!C){
        statement_3;
        switch(variable){
        case A:
            statement_1;
            break;
        case B: 
            statement_2;
            break;
    }
    

    or make use of both the if-statement and the switch?

    if(!C){
        statement_3;
    }
    switch(variable){
    case A:
        statement_1;
        break;
    case B: 
        statement_2;
        break;
    

提交回复
热议问题