根据用于指定的月份,打印该月份所属的季节.
一旦case匹配,就会顺序执行后面的程序代码,而不管后面的case是否匹配,直到遇见break,利用这一特性可以让好几个case执行统一语句.
3 4 5—spring(春天)
6 7 8—sunmer(夏天)
9 10 11—autumn(秋天)
12 1 2—winter(冬天)
public static void main(String[] args) { int x = 3; switch (x) { case 3: case 4: case 5: System.out.println("spring"); break; case 6: case 7: case 8: System.out.println("sunmer"); break; case 9: case 10: case 11: System.out.println("autumn"); break; case 12: case 0: case 1: System.out.println("winter"); default: System.out.println("ok"); break; } } |