What is a NumberFormatException and how can I fix it?

前端 未结 9 1352
渐次进展
渐次进展 2020-11-21 04:25
Error Message:
Exception in thread \"main\" java.lang.NumberFormatException: For input string: \"Ace of Clubs\"
    at java.lang.NumberFormatException.forInputString         


        
9条回答
  •  别那么骄傲
    2020-11-21 04:56

    The very first thing that threw me for a loop (no pun intended) was you were limiting the value to 1-13 when it needs to be 0-52. Also with your logic the value was always be higher. A better approach is with a number generator. Here is my code using a number generator (or Java Random):

    public static void main(String[] args) {
    
    String[] cards = { "Ace of Clubs", "1 of Clubs", "2 of Clubs",
            "3 of Clubs", "4 of Clubs", "5 of Clubs", "6 of Clubs",
            "7 of Clubs", "8 of Clubs", "9 of Clubs", "10 of Clubs",
            "Queen of Clubs", "King of Clubs", "Ace of Diamonds",
            "1 of Diamonds", "2 of Diamonds", "3 of Diamonds",
            "4 of Diamonds", "5 of Diamonds", "6 of Diamonds",
            "7 of Diamonds", "8 of Diamonds", "9 of Diamonds",
            "10 of Diamonds", "Queen of Diamonds", "King of Diamonds",
            "Ace of Hearts", "1 of Hearts", "2 of Hearts", "3 of Hearts",
            "4 of Hearts", "5 of Hearts", "6 of Hearts", "7 of Hearts",
            "8 of Hearts", "9 of Hearts", "10 of Hearts",
            "Queen of Hearts", "King of Hearts", "Ace of Spades",
            "1 of Spades", "2 of Spades", "3 of Spades", "4 of Spades",
            "5 of Spades", "6 of Spades", "7 of Spades", "8 of Spades",
            "9 of Spades", "10 of Spades", "Queen of Spades",
            "King of Spades" };
    
    Scanner scanner = new Scanner(System.in);
    Random rand = new Random();
    String response = "";
    int index = 0;
    int value = 0;  
    while (!response.equals("q") && index < 52) {
    
        // set next card value based on current set of cards in play
        if (cards[index].endsWith("Clubs")) {
            value = rand.nextInt(12);
        }
        if (cards[index].endsWith("Diamonds")) {
            value = rand.nextInt(12) + 13;
        }
        if (cards[index].endsWith("Hearts")) {
            value = rand.nextInt(12) + 26;
        }
        if (cards[index].endsWith("Spades")) {
            value = rand.nextInt(12) + 39;
        }
    
        // display card too user (NOTE: we use the random number not the index)
        System.out.println("Card is: " + cards[value]);
    
        // ask user what well the next card be
        System.out.println("Will the next card be higher or lower?, press q if you want to quit");
        response = scanner.nextLine();
    
        // display if user was right (NOTE: compared the random number to the current index)
        // ignore incorrect response and just continue
        if ((value > index && response.startsWith("h")) || (value < index && response.startsWith("l"))) {
            System.out.println("You answer was right, well done!");
        } else {
            System.out.println("You answer was wrong, try again!");
        }
    
        // continue loop
        index++;
    }
    }
    

    As for the NumberFormatException I believe Nicolas Filotto did a good job explaining that.

提交回复
热议问题