Learning punch 7: select statement switch

 

 

 

Which meet on a case from which a case started, if all are not met, in default to the last ending.

/ *
Switch statement Note:
the value 1, a plurality of the latter case can not be repeated. (Repeated value, the compiler error protection)
2, behind which only switch parentheses following data types:
basic data types: byte, short, char, int ( others can not)
reference Data Type: String String, enum enum (others can not)
. 3, it can be very flexible format Switch statement: before and after the order may be reversed, and the break statement may be omitted.
4, Jin Yan statement to prevent the penetration switch, in accordance with the format required to write the end of the case began bread.
"Match which case it is a position from which execution down until it encounters a break or the end of the whole"
* /

Practice Code:

public class Demo16Swith{
    public static void main(String[]args){
        int num = 1;
        switch(num){
            case 1:
                System.out.println("星期一");
                break;
            case 2:
                System.out.println("星期二");
                break;
            case 3:
                System.out.println("星期三");
                break;
            case 4:
                System.out.println("星期四");
                break;
            case 5:
                System.out.println("星期五");
                break;
            case 6:
                System.out.println("星期六");
                break;
            case 7:
                System.out.println("星期七");
                break;
        default:
            System.out.println("数据不合理");
            break;  //最后一个break语句可以省略,但是强烈建议加上,保证句子的严谨性
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/xiangxu-zhao/p/12374594.html