使用枚举值进行切换

时间:2018-11-09 16:27:18

标签: java enums

我的Aligment类别如下:

public enum Aligment
    {
        Evil,
        Neutral,
        Good,
        Undefined
    }

我想像这样在开关中使用这些值:

System.out.print("Choose you'r start up character" +
                     "1.Good" +
                     "2.Evil" +
                     "3.Neutral");
    //1  string alignmentChoice = scan.nextLine();
    //2  Aligment alignmentChoice = Aligment.Undefined;
    switch( aligmentChoice )
    {
       case Good:
        alignment = Aligment.Good;
        break;

       case Evil:
        alignment = Aligment.Evil;
        break;

       case Neutral:
        alignment = Aligment.Neutral;
        break;

       default:
        System.out.println("How did you manage to get here? You have broke the system.");
        break;

     }

我不确定如何使用// 1或// 2。谢谢您的帮助。

3 个答案:

答案 0 :(得分:4)

像这样使用它:

switch(Aligment.valueOf(alignmentChoise)) {
   case Evil: 
     alignment = Aligment.Evil;
     break;
}

注意: 如果找不到枚举常量,则会抛出IllegalArgumentException

答案 1 :(得分:2)

如果您将这种切换用例逻辑重构并将其移至枚举中的方法,则不再需要使用switch语句从字符串中获取枚举。

在下面的示例中,我添加了方法fromString(),它将接受字符串输入name,并与我们所有的枚举值进行比较(不区分大小写)。

如果找不到匹配的值,我们将抛出IllegalArgumentException

这里是示例:

public class AlignmentTest{

     public static void main(String[] args){

        String good = "good";
        String neutral = "NEUTRAL";
        String evil = "EvIl";
        String unknown = "unknown";

        Alignment alignment1 = Alignment.fromString(good);
        System.out.println("Alignment 1: " + alignment1);

        Alignment alignment2 = Alignment.fromString(neutral);
        System.out.println("Alignment 2: " + alignment2);

        Alignment alignment3 = Alignment.fromString(evil);
        System.out.println("Alignment 3: " + alignment3);

        Alignment alignment4 = Alignment.fromString(unknown);
        System.out.println("Alignment 4: " + alignment4);
     }

     public enum Alignment {   
        EVIL("Evil"),
        NEUTRAL("Neutral"),
        GOOD("Good");

        private String name;

        Alignment(String name) {
            this.name = name;
        }

        public String getName() {
            return this.name;
        }

        public static Alignment fromString(String name) {
            for (Alignment alignment : Alignment.values()) {
                if (alignment.name.equalsIgnoreCase(name)) {
                    return alignment;
                }
            }
            throw new IllegalArgumentException("No alignment with name " + name + " found");
        }
    }
}

这将输出以下内容:

Alignment 1: GOOD
Alignment 2: NEUTRAL
Alignment 3: EVIL
Exception in thread "main" java.lang.IllegalArgumentException: No alignment with name unknown found
    at AlignmentTest$Alignment.fromString(AlignmentTest.java:44)
    at AlignmentTest.main(AlignmentTest.java:19)

答案 2 :(得分:0)

在枚举中创建一个映射(静态映射),以保存用户要提供给枚举的名称之间的映射。

public enum Alignment {
    Evil("Evil"),
    Neutral("Neutral"),
    Good("Good"),
    Undefined("Undefined");

    private static final Map<String, Alignment> MAPPINGS = new HashMap<>();
    static {
        for (Alignment alignment : Alignment.values()) {
            MAPPINGS.put(alignment.getName(), alignment);
        }
    }

    private String name;
    Alignment(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public Alignment getAlignmentForName(String name) {
        return MAPPINGS.get(name);
    }

现在,您可以使用getAlignmentForName将用户输入映射到枚举。它将为无效值返回null

这样做的好处是,添加新的枚举实例时无需更改任何代码...对于您的情况,必须添加适当的切换用例。

注意:枚举字段的名称必须按照conventions用大写字母表示。