switch多选择结构
switch语句中的变量类型可以是
byte,short,int或者char
package com.ahez.www;
public class java6 {
public static void main(String[] args) {
char grade = 'C';
switch (grade){
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("及格");
break;
case 'D':
System.out.println("再接再厉");
break;
case 'E':
System.out.println("挂科");
break;
default:
System.out.println("未知");
}
}
}
从java se7开始,switch支持字符串String类型了
同时case标签必须为字符串常量或字面量
package com.ahez.www;
public class java7 {
public static void main(String[] args) {
String name = "PPP";
switch (name) {
case "啊盒":
System.out.println("啊盒");
break;
case "AHez":
System.out.println("AHez");
break;
case "张小千":
System.out.println("张小千");
break;
default:
System.out.println("弄啥呢");
}
}
}