Java使用用户输入来确定枚举值

  • 本文关键字:枚举 用户 Java java enums
  • 更新时间 :
  • 英文 :


我目前正在硬编码一个enum值,该值通过switch语句运行。是否可以根据用户输入来确定枚举。

Choice month = Choice.D;

我可以在这里使用用户输入而不是硬编码值D吗?

package partyAffil;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class partyAffil {
public static void main(String[] args) {
System.out.println("Choose from the following menu");
System.out.println("D, R, or I");   
String choice = getInput("please choose a letter.");

    Choice month = Choice.D;

    switch(month){ 
    case D:
        System.out.println("You get a Democratic Donkey");
        break;
    case R:
        System.out.println("You get a Republican Elephant");
        break;
    case I:
        System.out.println("You get an Independent Person");
        break;
    default:
        System.out.println("You get a unicorn");
        break;
        }
}
    public enum Choice{
        D, R, I;
    }
   private static String getInput(String prompt)
        {
        BufferedReader stdin = new BufferedReader(
                new InputStreamReader(System.in));
        System.out.print(prompt);
        System.out.flush();
        try{
            return stdin.readLine();
        } catch (Exception e){
            return "Error: " + e.getMessage();
        }
    }
}

每个枚举常数在其声明中都有自己的名称。特定枚举的静态方法valueOf按名称返回该类型的枚举常量。


因此,相反:

Choice month = Choice.D;

就用这个:

Choice month = Choice.valueOf(choice);

如果您在输入而不是月份上创建开关(或者两者都创建,如果它们必须单独实现的话)会怎么样?:

Choice month;

switch(choice.toUpperCase()){ 
case 'D':
    month = Choice.D
    System.out.println("You get a Democratic Donkey");
    break;
    ...
    }

更好的是,我相信你甚至可以设置枚举中的字符值:

public enum Choice{
        D('D'), R('R'), I('I');
    }

这样,你仍然可以使用case D:而不是case 'D':

(这个不太确定,我更习惯基于c的语言)

以上给出的答案可能有所帮助。如果您不是专家,请使用下面的代码来理解这些步骤。

    public void run() throws Exception
{
        switch (menu() ) //calling a menu method to get user input.
        {
            case 1:
              //fyi, make sure you have all the methods in your code that is given for each menu choice.
              //you can have print statement in your method.
                  // e.g.       System.out.println("You get a Democratic Donkey");
                method1();
                break;
            case 2:
                method2();
                break;
            case 3:
                method3();
                break;      
            case 0:
                return;  
            //following default clause executes in case invalid input is received.
            default:
                System.out.println ( "Unrecognized option" );
                break;
            }
}
private int menu()
{   
    //list of menu items.
    System.out.println ("1. insert appropriate description for your menu choice 1.");
    System.out.println ("2. ");
    System.out.println ("3. ");
    System.out.println ("4. ");
    return IO_Support.getInteger(">>    Select Option: ", "Unrecognized option"); //you can use scanner to get user input. I have user separate class IO_Support that has getInteger methods to validate int user input. the text "unrecognized option" is what the message that i want to print if the option is integer but not valid choice.
}

相关内容

  • 没有找到相关文章

最新更新