lundi 28 janvier 2019

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.

Aucun commentaire:

Enregistrer un commentaire