int num = 1; //这是值
switch(num){
case 1:
case 2:
case 4:
System.out.println("执行方法1");
break;
case 3:
case 5:
System.out.println("执行方法2");
break;
default:
System.out.println("没有可执行的方法.");
}
不是这么写的么:
switch(i){
case 1:
case 2:
case 4:
function1();
break;
case 3:
case 5:
function2();
break;
default:
break;
}
那还用switch干嘛 直接
if(条件1 || 条件2 || 条件4){
方法1
} else if (条件3 || 条件5){
方法2
}
像这样写就行了。
switch(i){
case 1:
case 2:
case 4:System.out.println("aa");break;
case 3:
case 5:System.out.println("bb");break;
default :System.out.println("cc");
}
估计还是每个条件的查,或者还不如用if语句,代码量会减少点