输入用于切换语句结果的枚举

时间:2016-02-18 20:57:01

标签: java enums switch-statement

所以在这里我要求用户输入命中,站立,投降或退出。我将这些作为枚举,以及NOTVALID。

enum Decisions

{
    HIT, STAND, SURRENDER, QUIT, PLAY, NOTVALID;
    // NOTVALID is used to re-ask to play again
}

因此,如果用户提出例如" blah"而不是其中一个有效选项,我希望它将其视为NOTVALID,并在switch语句中执行NOTVALID中的代码。我怎样才能做到这一点?实际上,当我运行程序时,它返回错误:

Exception in thread "main" java.lang.IllegalArgumentException: No enum constant BlackJack.Decisions.BLAH

如果不是其他枚举中的任何内容,我该如何处理NOTVALID呢?

System.out.println("Do you want to hit, stand, surrender, or quit?");

        String decisionInput;

        decisionInput = keyboard.nextLine().toUpperCase();

        decision = Decisions.valueOf(decisionInput);

        switch (decision)
        {
            case HIT: 
                Random playerCard = new Random();

                cardPick = playerCard.nextInt(13);

                playerSum = playerSum + getCardValue(cardPick);

                System.out.println("Card: " + (Card.values()[cardPick]) + ". The sum of your hand is: " + playerSum + "."); 

                System.out.println("The hole card was " + (Card.values()[cardPick2]) + ". The house has: " + houseSum + ".");

                findWinner(playerSum, houseSum, decision);

                System.out.println("Do you want to play again? Type PLAY or QUIT.");

                decision = Decisions.valueOf(keyboard.nextLine().toUpperCase());

                break;

            case STAND:
            case SURRENDER:

                findWinner(playerSum, houseSum, decision);

                System.out.println("Do you want to play again? Type PLAY or QUIT.");

                decision = Decisions.valueOf(keyboard.nextLine().toUpperCase());

                break;

            case QUIT:

                System.out.println("Thanks for playing!");

                System.exit(0);

                break;

            case NOTVALID:

                System.out.println("Invalid input. Try again: hit, stand, surrender, or quit?");

                decision = Decisions.valueOf(keyboard.nextLine().toUpperCase());

            }   
        }
    }       
}

2 个答案:

答案 0 :(得分:2)

定义一个为无法识别的输入返回NOTVALID的新方法。最简洁的方法是捕获正在抛出的IllegalArgumentException。

enum Decisions{
     HIT, STAND, SURRENDER, QUIT, PLAY, NOTVALID;

     public static Decisions asDecision(String input) {
         try {
             return Decisions.valueOf(input);
         } catch(IllegalArgumentException e) {
             return Decisions.NOTVALID;
         }
     }
}

如果这会冒犯您使用try..catch的敏感度,您可以自己手动完成。

enum Decisions{
         HIT, STAND, SURRENDER, QUIT, PLAY, NOTVALID;

         public static Decisions asDecision(String input) {
             for(Decisions d : values()) {
                 if(d.name().toLowerCase().equals(input.toLowerCase()) {
                     return d;
                 }
             }
             return Decisions.NOTVALID;
         }
    }

无论哪种方式,请改用新方法。

不允许定义新方法,您可以在try..catch中围绕作业:

    String decisionInput;
    decisionInput = keyboard.nextLine().toUpperCase();
    try{
        decision = Decisions.valueOf(decisionInput);
    } catch(IllegalArgumentException e){
        decision = Decisions.NOTVALID;
    }
    // Rest of your code...

答案 1 :(得分:0)

您可以使用default:代替NOTVALID。它类似于else所有其他情况,它将默认为该逻辑。

default:
    System.out.println("Invalid input. Try again: hit, stand, surrender, or quit?");
    decision = Decisions.valueOf(keyboard.nextLine().toUpperCase());