//switch结构
class SwitchDemo1
{
public static void main(String[] args)
{
/*
适用于对多个整型值(byte short int char)判断.if更强大
case只是一个入口,case间互斥
default通常放在最后,不需要break
*/
int weekday = 1;
switch (weekday)
{
case 1 : System.out.println("周1"); break;
case 2 : System.out.println("周2"); break;
case 3 : System.out.println("周3"); break;
case 4 : System.out.println("周4"); break;
case 5 : System.out.println("周5"); break;
case 6 : System.out.println("周6"); break;
case 7 : System.out.println("周7"); break;
default: System.out.println("Hello World!");
}
System.out.println("Hello World!");
//支持 char类型的数据
char ch = 'A';
switch (ch)
{
case 'A': System.out.println("A"); break;
}
//穿透现象 判断是工作日还是休息日
int daydemo = 1;
switch (daydemo)
{
case 1 :
case 2 :
case 3 :
case 4 :
case 5 : System.out.println("工作日"); break;
case 6 :
case 7 : System.out.println("休息日"); break;
default: System.out.println("Hello World!");
}
}
}
if与switch结构的选择