我已经按照如下方式上课了:
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这样使用它。预先感谢您的帮助。
最佳答案
像这样使用它:
switch(Aligment.valueOf(alignmentChoise)) {
case Evil:
alignment = Aligment.Evil;
break;
}
注意 :
如果没有找到枚举常量,将抛出
IllegalArgumentException
。