今天在看程式碼時意外發現原來switch case可以這樣用,以前好像都沒注意到說呢!

看來他跟if else比起來也有不同的用途呢?

class Month{
    public static void main(String[] args){

        int test1=1;

        int test2=8;

        switch(test2){
        case 1:
        case 2:
        case 3:
            print("1 to 3");
            break;
        case 4:
        case 5:
        case 6:
            print("4 to 6");
            break;
        case 7:
        case 8:
        case 9:
            print("7 to 9");
            break;
        case 10:
        case 11:
        case 12:
            print("10 to 12");
            break;
        default:
            System.out.println("error");
        }
    }

    
    public static void print(String a){
        System.out.println("The unmber is in "+a +"scope");
    }
}

創作者介紹
創作者 roczen2007 的頭像
roczen2007

roczen2007

roczen2007 發表在 痞客邦 留言(0) 人氣( 82 )