实例1:
public static void main(String[] args) {
int lable = 2;
switch (lable) {
case 1:
System.out.println("lable的值是1");
break;
case 2:
System.out.println("lable的值是1");
break;
case 3:
System.out.println("lable的值是1");
break;
default:
System.out.println("默认");
}
}
输出为:lable的值是1
实例2:
String str = "XiaoMing";
switch (str) {
case "LiSi":
System.out.println("str的值是LiSi");
break;
case "ZhangSan":
System.out.println("str的值是ZhangSan");
break;
case "XiaoMing":
System.out.println("str的值是XiaoMing");
break;
default:
System.out.println("默认");
}
}
输出为:str的值是XiaoMing
注意:
switch 语句中的变量类型可以是: byte、short、int 或者 char 或者 String 字符串。同时 case 标签必须为字符串常量或字面量。