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

标签 java enums switch-statement runtime-error

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

相关文章:

java - 为什么 Java 在连续整数上的开关似乎在增加的情况下运行得更快?

java - Switch 语句未正确执行

java - 使用 java 的 KeyPairGenerator 的 openssl 等效项是什么?

java - 获取 404 虽然我的应用程序已部署

c# - 如何替换/覆盖同一命名空间中的类

powershell - 使用另一个脚本中定义的枚举

objective-c - 用于对象的 Switch 语句的高性能 Objective C 替代方案

java - 我正在尝试为 JPanel 窗口背景着色。但是执行后我得到的 JPanel 窗口没有任何颜色。代码中出现什么错误?

java - Tomcat JSP 错误 500

java - 枚举与类加载器