public class SwitchTest {
public static void main(String[] args){
//switch语句练习
int a = 3 ;
int b = 4 ;
switch (a){
default :
b ++;
case 4:
b ++ ;
break ;
case 5:
b ++ ;
break ;
}
//b的值是多少
System.out.println("b:"+b);
System.out.println("-------------------------");
int x = 3 ;
int y = 4 ;
switch (x){
default:
y ++ ;
case 3 :
y ++ ; //y= 5
case 5:
y++ ; // y= 6
}
System.out.println("y:"+y);
}
}
1.4利用case穿透
例
import java.util.Scanner ;
public class SwtichTest4 {
public static void main(String[] args){
//创建键盘录入对象
Scanner sc = new Scanner(System.in) ;
//提示并录入数据
System.out.println("请您输入月份值:") ;
int month = sc.nextInt() ;
//switch语句
//利用:case穿透现象
switch (month){
case 3:
case 4:
case 5:
System.out.println("春季");
break ;
case 6:
case 7:
case 8:
System.out.println("夏季");
break ;
case 9:
case 10:
case 11:
System.out.println("秋季");
break ;
case 12:
case 1:
case 2:
System.out.println("冬季");
break ;
default:
System.out.println("对不起,您输入的数据不合法!!") ;
break ;
}
}
}