gpt4 book ai didi

java - 编译器不允许使用枚举名称

转载 作者:行者123 更新时间:2023-12-02 05:55:26 25 4
gpt4 key购买 nike

所以我在程序中声明枚举时遇到了麻烦,所以我从sun网站示例中复制了代码,起初编译器比1.5旧,所以我更新了它,错误改为:

Exception in thread "main" java.lang.NoSuchMethodError: examples.Day.values()[Lexamples/Day;
at examples.EnumTest.$SWITCH_TABLE$examples$Day(EnumTest.java:7)
at examples.EnumTest.tellItLikeItIs(EnumTest.java:15)
at examples.EnumTest.main(EnumTest.java:37)

我的代码:

import java.lang.Enum;
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY ;
}
public class EnumTest {
Day day;

public EnumTest(Day day) {
this.day = day;
}

public void tellItLikeItIs() {
switch (day) {
case MONDAY:
System.out.println("Mondays are bad.");
break;

case FRIDAY:
System.out.println("Fridays are better.");
break;

case SATURDAY:
case SUNDAY:
System.out.println("Weekends are best.");
break;

default:
System.out.println("Midweek days are so-so.");
break;
}
}

public static void main(String[] args) {
EnumTest firstDay = new EnumTest(Day.MONDAY);
firstDay.tellItLikeItIs();
EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
thirdDay.tellItLikeItIs();
EnumTest fifthDay = new EnumTest(Day.FRIDAY);
fifthDay.tellItLikeItIs();
EnumTest sixthDay = new EnumTest(Day.SATURDAY);
sixthDay.tellItLikeItIs();
EnumTest seventhDay = new EnumTest(Day.SUNDAY);
seventhDay.tellItLikeItIs();
}
}

最佳答案

您的枚举声明是错误的。将整个声明作为内部类放入 EnumTest 类中,删除 public 说明符,或者将其放入单独的文件中。

关于java - 编译器不允许使用枚举名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23137252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com