注意:
1.当遇到break之后就停止switch了,break也可以不写,如果不写break就会发生case穿透;
2.default也可以没有;
3.switch后面可以放byte、short、char、int类型,只要可以自动转换为int类型的都可以,在jdk7之后可以放String类型。
import java.util.Scanner;
public class switchDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int month = sc.nextInt();
switch (month) {
case 12:
case 1:
case 2:
System.out.println("冬天");
break;
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;
default:
System.out.println("月份不存在");
break;
}
}
}