带有Java中的字符串和枚举的switch-case

问题描述

我想使用switch-case来检查Java中具有enum值的字符串,所以我做到了:

public enum DemoEnumType {

    ALL(""),TOP("acb"),BOTTOM("def");

    private String code;

    DemoEnumType(String code) {
        this.code = code;
    }

    public String code() {
        return this.code;
    }


}

当我运行此代码时,它将引发异常:

public class Demo {
    public static void main(String[] args) {
        DemoEnumType typeValue = DemoEnumType.valueOf("acb");
        
        switch (typeValue){
            case ALL:
                System.out.print("match");
            case BOTTOM:
                System.out.print("match");
            case TOP:
                System.out.print("match");
        }

    }
}

项目:

线程“ main”中的异常java.lang.IllegalArgumentException:没有枚举常量package.DemoEnumType.acb。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)