switchcase在枚举中怎样使用

avatar
作者
筋斗云
阅读量:1

在枚举中使用switch-case语句时,可以根据枚举常量的不同取值来执行不同的逻辑代码。以下是一个示例:

enum Day {     MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }  Day day = Day.MONDAY;  switch (day) {     case MONDAY:         System.out.println("Today is Monday");         break;     case TUESDAY:         System.out.println("Today is Tuesday");         break;     case WEDNESDAY:         System.out.println("Today is Wednesday");         break;     case THURSDAY:         System.out.println("Today is Thursday");         break;     case FRIDAY:         System.out.println("Today is Friday");         break;     case SATURDAY:         System.out.println("Today is Saturday");         break;     case SUNDAY:         System.out.println("Today is Sunday");         break;     default:         System.out.println("Invalid day"); } 

在上面的示例中,我们声明了一个枚举类型Day,并将day赋值为Day.MONDAY。然后使用switch-case语句根据day的取值来输出不同的信息。如果day的取值不在枚举常量中,则会执行default语句块。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!