day03-流程控制语句-选择流程-switch的注意事项
This commit is contained in:
45
day03/src/com/inmind/switch02/Demo06.java
Normal file
45
day03/src/com/inmind/switch02/Demo06.java
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package com.inmind.switch02;
|
||||||
|
/*
|
||||||
|
case的穿透性
|
||||||
|
在switch语句中,如果case的后面不写break,将出现穿透现象,也就是不会在判断下一个case的值,
|
||||||
|
直接向后运行,直到遇到break,或者整体switch结束。
|
||||||
|
|
||||||
|
|
||||||
|
注意:1.switch中进行选择判断的数据类型是有范围的,只能是byte,short,int,char,枚举,String
|
||||||
|
2.case穿透:break可以不写,当某一个case满足条件,就开始执行与语句体,如果没有遇到break
|
||||||
|
会继续向下运行,直到遇到break结束或直到switch语句结束
|
||||||
|
*/
|
||||||
|
public class Demo06 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// 请使用switch 来实现指定的月份是哪个季节????
|
||||||
|
int month = 12;
|
||||||
|
|
||||||
|
switch (month) {
|
||||||
|
case 3:
|
||||||
|
case 4:
|
||||||
|
case 5:
|
||||||
|
System.out.println("春季");
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
System.out.println("case6");
|
||||||
|
case 7:
|
||||||
|
System.out.println("case7");
|
||||||
|
case 8:
|
||||||
|
System.out.println("case8");
|
||||||
|
System.out.println("夏季");
|
||||||
|
break;
|
||||||
|
case 9:
|
||||||
|
case 10:
|
||||||
|
case 11:
|
||||||
|
System.out.println("秋季");
|
||||||
|
break;
|
||||||
|
case 12:
|
||||||
|
case 1:
|
||||||
|
case 2:
|
||||||
|
System.out.println("冬季");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user