gpt4 book ai didi

java - 枚举和开关帮助。 Java学习者

转载 作者:行者123 更新时间:2023-12-01 08:00:13 25 4
gpt4 key购买 nike

我是一名Java自学者,被这个问题困扰了。尝试过不合理的组合和可能的解决方案,但遇到了更多的错误。

public class SeasonsSwitch {
enum Season {
WINTER,
SPRING,
SUMMER,
FALL
}
public static void main(String[] args){

String currentSeason;
currentSeason = TextIO.getWord();
switch (currentSeason){
case WINTER:
TextIO.put("Decemeber, January, February");
break;
case SPRING:
TextIO.put("March, April, May");
break;
case SUMMER:
TextIO.put("June, July, August");
break;
case FALL:
TextIO.put("September, October, November");

}

}
}

error. cannot find symbol
case WINTER
error. cannot find symbol
case SPRING
error. cannot find symbol
case SUMMER
error. cannot find symbol
case FALL

最佳答案

使用Season.valueOf将枚举常量的字符串表示形式转换为枚举常量。

Season s = Season.valueOf(currentSeason);
switch (s){

Enum.valueOf tutorial

The java.lang.Enum.valueOf() method returns the enum constant of the specified enumtype with the specified name. The name must match exactly an identifier used to declare an enum constant in this type.

关于java - 枚举和开关帮助。 Java学习者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25898214/

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