Unable to get behaviour of Switch case in java

后端 未结 8 705
迷失自我
迷失自我 2021-01-23 14:07

I have written small code in java 6

public class TestSwitch{

public static void main(String... args){
    int a = 1;
    System.out.println("start");
          


        
8条回答
  •  清酒与你
    2021-01-23 14:37

    int a = 1;
    System.out.println("start");
    switch (a) {
    case 1: {
        System.out.println(1);
        break;
    }
    case 3: {
        System.out.println(3);
        break;
    }
    case 4: {
        System.out.println(4);
        break;
    }
    case 2: {
        System.out.println(2);
        break;
    }
    case 5: {
        System.out.println(5);
        //no break will fall through and print 7 too 
    }
    case 7: {
        System.out.println(7);
        break;
    }
    default:{
        System.out.println("none");
    }
    
    }
    

提交回复
热议问题