switch注意事项
1.case 后面只能是常量,不能是变量,而且多个。
2.case后面的值不能出现相同的。
3.default可以省略,但不建议,除非判断的值是固定的。
4.default可以出现在switch语句的任意位置
5.switch语句的结束条件:遇到break或者执行到程序末尾。
6.break可以忽略,但结果可能不是想要的,可会出现case穿透
package javase; import java.util.Scanner; //实现键盘录入月份,输出对应的季节 public class SwitchDemo4 { public static void main(String [] args) {//键盘录入数据Scanner sc = new Scanner(System.in);System.out.println("请输入月份");int month = sc.nextInt();switch(month) {/*case 1:System.out.println("冬季");break;case 2:System.out.println("春季");break;case 3:System.out.println("春季");break;case 4:System.out.println("春季");break;case 5:System.out.println("夏季");break;case 6:System.out.println("夏季");break;case 7:System.out.println("夏季");break;case 8:System.out.println("秋季");break;case 9:System.out.println("秋季");break;case 10:System.out.println("秋季");break;case 11:System.out.println("冬季");break;case 12:System.out.println("冬季");break;default :System.out.println("没有这个月份");*/case 2:case 3:case 4:System.out.println("春季");break;case 5:case 6:case 7:System.out.println("夏季");break;case 8:case 9:case 10:System.out.println("秋季");break;case 11:case 12:case 1:System.out.println("冬季");break;}//但这样太麻烦了,所以可以使用case穿透 } }
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677