Control flow textbook series (two) - java switch statement
more, click here to learn, sign up for a
switch statement is equivalent to another expression if else's
step 1: switch
Example. 1: Switch
Switch may be used byte, short, int, char, String, enum
Note: the end of each expression, there should be a break;
Note: String before Java1.7 is not supported, Java began to support the switch from the 1.7 with a String, the String is compiled into a hash value, but it is still an integer
Note: enum is an enumeration type, the enumeration has explained in detail in chapter
public class HelloWorld {
public static void main(String[] args) {
//如果使用if else
int day = 5;
if (day==1)
System.out.println("星期一");
else if (day==2)
System.out.println("星期二");
else if (day==3)
System.out.println("星期三");
else if (day==4)
System.out.println("星期四");
else if (day==5)
System.out.println("星期五");
else if (day==6)
System.out.println("星期六");
else if (day==7)
System.out.println("星期天");
else
System.out.println("这个是什么鬼?");
//如果使用switch
switch(day){
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("这个是什么鬼?");
}
}
}