switch结构学习应注意的细节
要点1、表达式的类型可以是:byte、short、int、char、枚举和字符串
要点2、case值应与表达式的数值类型保持一致(表达式的值可能在case值中找到)
要点3、default的位置可以改变
经典例题:
下列选项中关于变量x的定义,( BD )可使以下switch语句编译通过。(选择二项)
switch(x) {
case 100 :
System.out.println("One hundred");
break;
case 200 :
System.out.println("Two hundred");
break;
case 300 :
System.out.println( "Three hundred");
break;
default :
System.out.println( "default");
}
A double x = 100;
B. char x = 100;
C. String x = "100";
D. int x = 100;