用Java枚举一周中的几天

要将枚举设置为星期几,请将它们设置为常量

enum Days {
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}

现在创建对象并设置上述常量-

Days today = Days.Wednesday;
Days holiday = Days.Sunday;

以下是一个例子-

示例

public class Demo {
   enum Days {
      Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
   }
   public static void main(String[] args) {
      Days today = Days.Wednesday;
      Days holiday = Days.Sunday;
      System.out.println("Today = " + today);
      System.out.println(holiday+ " is holiday");
   }
}

输出结果

Today = Wednesday
Sunday is holiday