因此,我在这里要求用户输入击打,站立,投降或退出的信息。我将这些作为枚举,以及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());

            }
        }
    }
}

最佳答案

定义一个新方法,该方法为无法识别的输入返回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;
         }
    }


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

在不被允许定义新方法的情况下,您可以将您的分配包含在try..catch中,如下所示:

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

09-19 01:04