我是一名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){
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/