gpt4 book ai didi

Java 月份枚举

转载 作者:行者123 更新时间:2023-12-04 16:29:35 25 4
gpt4 key购买 nike

大家好,我是 Java 新手,所以我不太了解,您能尽可能简单地帮助我吗?这是我的代码,我收到了关于月份(int)的错误,这意味着它无法导入库.

public enum Month {
January(1), February(2), March(3), April(4),May(5),June(6), July(7), August(8), September(9), October(10), November(11), December(12)
}

ERROR:Description Resource Path Location Type The constructor Month(int) is undefined Month.java /tb00594_comp1027_formative2/src/tb00594_comp1027_formative2 line 4 Java Problem

WARNING:Description Resource Path Location Type Build path specifies execution environment JavaSE-1.7. There are no JREs installed in the workspace that are strictly compatible with this environment. tb00594_comp1027_formative2 Build path JRE System Library Problem

所以如果你能尽快帮助我,我会很高兴的。

最佳答案

Enum 基本上是最后一组有效选项的声明(在您的情况下是几个月)。但它仍然是Java Class~Object。

您的错误字面意思是您缺少 Java 类的构造函数,而您想为每个枚举提供特定属性。我猜你想在日历中添加月份顺序。您需要做的只是声明类和枚举构造函数的属性。

public enum Month {
JANUARY(1), FEBRUARY(2), MARCH(3), APRIL(4),MAY(5),JUNE(6), JULY(7), AUGUST(8), SEPTEMBER(9), OCTOBER(10), NOVEMBER(11), DECEMBER(12);

private int monthOrder;

public Month (int monthOrder) {
this.monthOrder = monthOrder;
}

public int getMonthOrder() {
return this.monthOrder;
}
}

关于Java 月份枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53279561/

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