case标签
case标签可以是:
常量表达式:char,byte,short,int
枚举常量
字符串字面量
当在switch语句中使用枚举常量时,不用在每个标签中指明枚举名,可以由switch的表达式值确定。
代码实现
size.java
package hello;
public enum size {
small,medium,large,xlarge;
}
clothe.java
package hello;
public class clothe {
public static void main(String[] args){
size sz=size.large;
switch (sz){
case small:
System.out.println("small");
break;
case medium:
System.out.println("medium");
break;
case large:
System.out.println("large");
break;
default:
System.out.println("xlarge");
}
}
}
运行结果:

本文介绍了Java中如何使用case标签,特别是与枚举常量配合使用的情况。在switch语句中,case标签可以接受常量表达式、枚举常量和字符串字面量。特别指出,当使用枚举常量时,无需在每个case中指定枚举类型名称,因为switch表达式的值会自动匹配。同时,提供了相关的代码示例(size.java和clothe.java)以展示其实际应用。
1245

被折叠的 条评论
为什么被折叠?



