public class TestSwitch{
public static void mian(String[] args){
int i = 4;
switch(i){ //Java中switch只能测探int类型(char,short,byte类型可以自动装换为int类型)
case 1 :
System.out.println("f");
break;
case 2 :
System.out.Println("f");
break; //加上break; 防止case穿透效果
case 3 :
System.out.println("f");
break;
case 4 :
System.out.println("t");
break;
default: //default可以省略,但不推荐省略
System.out.println("error");
}
}
}
多条switch语句可以合并
public class TestSwitch{
public static void mian(String args){
int i = 4;
switch(i){
case 1 :
case 2 :
case 3 :
System.out.println("f");
break;
case 4 :
System.out.println("t");
break;
default :
System.out.println("error");
}
}
}