Example 1: Java Enum enum Size { SMALL, MEDIUM, LARGE, EXTRALARGE } class Main { public static void main(String[] args) { System.out.println(Size.SMALL); System.out.println(Size.MEDIUM); } } Run Code Output SMALL MEDIUM As we can see from the above example, we use the enum name...
public class EnumExample { // 定义一个枚举类型 public enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } public static void main(String[] args) { // 使用枚举类型 Day today = Day.MONDAY; // 打印枚举常量 System.out.println("Today is " + today); // 使用switch...
importjava.util.EnumMap;publicclassExample{publicenumGrade{A,B,C,D,F}publicstaticvoidprocessGrades(EnumMap<Grade,Integer>grades){for(Gradegrade:grades.keySet()){intscore=grades.get(grade);System.out.println("Processing grade: "+grade+", score: "+score);}}publicstaticvoidmain(String[]args){E...
enum Season { SPRING, SUMMER, AUTUMN, WINTER } public class EnumExample { public static void main(String[] args) { Season currentSeason = Season.SUMMER; switch(currentSeason) { case SPRING: System.out.println("It is spring season."); break; case SUMMER: System.out.println("It is summer...
publicclassEnumExample{publicstaticvoidmain(String[]args){Daytoday=Day.MONDAY;System.out.println("Today is "+today);}} 1. 2. 3. 4. 5. 6. 在这个示例中,我们定义了一个EnumExample类,通过Day.MONDAY来表示今天是星期一。当我们运行这段代码时,会输出Today is MONDAY。
publicclass EnumExample { public static void main(String[] args) { MyColor color0 = MyColor.RED; switch(color0) { case RED: System.out.println("红色"); break; case BLUE: System.out.println("蓝色"); break; case GREEN: System.out.println("绿色"); ...
packagecn.juwatech.application;importcn.juwatech.enums.Day;publicclassEnumExample{publicstaticvoidmain(String[] args){Daytoday=Day.MONDAY;switch(today) {caseMONDAY: System.out.println("今天是星期一");break;caseTUESDAY: System.out.println("今天是星期二");break;caseWEDNESDAY: ...
Defined Months Enum class Each Enum constant contains a String and integer value to it #How to define an enum in another class? It is an example of an enum declaration in other classes. publicclassMonthsClass{publicenumMonths{JAN(1),FEB(2),MAR(3),APRIL(4),MAY(5),JUN(6),JUL(7),AUG...
public class EnumExample { public static void main(String[] args) { Day[] days = Day.values(); for (Day day : days) { System.out.println(day); } } } 在上面的示例中,我们定义了一个名为Day的枚举类型,它表示一周的天数。然后,在main()方法中,我们使用values()方法获取Day枚举类型的所有值...
public class Example {enum Weekday {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY}public static void main(String[] args) {Weekday day = Weekday.TUESDAY;switch (day) {case MONDAY:System.out.println("今天是星期一,需要早起工作。");break;case TUESDAY:System.out.println("今天是...