我是一名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
tutorialjava.lang.Enum.valueOf()方法返回具有指定名称的指定枚举类型的枚举常量。名称必须与用于声明此类型的枚举常量的标识符完全匹配。