Java Switch Statement

后端 未结 8 2256
南笙
南笙 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:13

    Here u must use if statement in this way because in switch there are normally default value also.

        if (letter == 'A' || letter == 'B') { System.out.println("Statement 3 ");}
    
    switch (letter) {
                case 'A':
                    System.out.println("Statement 1 ");
                    break;
    
                case 'B':
                    System.out.println("Statement 2 ");
                    break;
    
                case 'C':
                    System.out.println();
                    break;
                default:
                    System.out.println("You entered wrong value");
    }       
    

提交回复
热议问题