简体   繁体   中英

How to add else statement if they put a string on an int input variable?

whenever I run this code, if I type a string as my input, I get an error on line 11. Help? I am not sure how to run an else statement or something asking what if they put a string rather than an integer (choice variable). I am new, so much help would be appreciated!

Here is my code:

import java.util.Scanner;

public class rockPaper {
    public static void main(String args[]) {
        System.out.println("Hello world");
        int rock = 0;
        int paper = 1;
        int scissors = 2;
        Scanner input = new Scanner(System.in);
        System.out.println("Rock, Paper, or Scissors(0, 1, or 2)? Enter your integer: ");
        int choice = input.nextInt();
        int aIChoice = (int) (Math.random() * 3);
        if (choice == rock) {
            switch (aIChoice) {
            case 0:
                System.out.println("AI chose rock.");
                System.out.println("Rock ties with rock!");
                break;
            case 1:
                System.out.println("AI chose paper.");
                System.out.println("Fail. Paper trumps rock.");
                break;
            case 2:
                System.out.println("AI chose scissors.");
                System.out.println("You win! Rock trumps scissors.");
                break;
            default:
                break;
            }

        } else if (choice == paper) {
            switch (aIChoice) {
            case 0:
                System.out.println("AI chose rock.");
                System.out.println("You win! Paper trumps rock.");
                break;
            case 1:
                System.out.println("AI chose paper.");
                System.out.println("Paper ties with paper!");
                break;
            case 2:
                System.out.println("AI chose scissors.");
                System.out.println("Fail. Scissors trumps paper.");
                break;
            default:
                break;
            }
        } else if (choice == scissors) {
            switch (aIChoice) {
            case 0:
                System.out.println("AI chose rock.");
                System.out.println("Fail. Rock trumps scissors.");
                break;
            case 1:
                System.out.println("AI chose paper.");
                System.out.println("You win! Scissors trumps paper.");
                break;
            case 2:
                System.out.println("AI chose scissors.");
                System.out.println("Scissors ties with scissors!");
                break;
            default:
                break;
            }
        } else {
            System.out.println("Nope!");
        }
        input.close();
    }

}

As stated above, if I run the code, and I type in any letters(a string), I get an error referencing to line number eleven. I am not sure what I should do, because obviously as I've mentioned adding an else statement to all of this does not ensure the "nope" if they entered a string.

Usually you would not accept a string input. If you really need to for some reason, you could replace the input line with String choice = input.next() then do whatever tests you need to with it. After you confirm it's the input you want refer to this post to convert from a String to int.

Not quite sure what do you expect in case user provides invalid input, but, I think, reasonable option would be to check the input and print error message to the user if input is not a valid integer. For this just add this code below instead of this line in your code: int choice = input.nextInt();

int choice = -1;
if (input.hasNextInt()) {
  choice = input.nextInt();
} else {
  System.out.println("Please provide valid integer input: Rock, Paper, or Scissors(0, 1, or 2)?");
}

PS Also it would be good to check if your integer is in range [0,2].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM