简体   繁体   中英

Why does my program print out the exception statement endlessly give bad user input?

Ive no idea if my title made sense but here is the code ©

import java.util.InputMismatchException;
import java.util.Scanner;
public class Gussing {
    public static void theGame(Scanner input){
        int randomNum= (int)(Math.random()*101);//randomizes a number between 0-100 inclusive of both
        System.out.println(randomNum); //for debugging purposes
        int attemptCounter = 0; //counts how many attempts the user make
        System.out.print("Welcome to the guess-the number game! Enter your guess: ");

        while(true){
            System.out.println("here is bad input");
            try{
                System.out.println("here is after the bad input");
                int userInput= input.nextInt();
                if (userInput==randomNum) //when usr input and generated random number are equal we print how many attempts
                {
                    attemptCounter++;
                    System.out.println("Congrats you made the right guess after "+ attemptCounter + " attempts!");
                    break;

                }

                if(userInput<randomNum){
                    attemptCounter++;
                    System.out.print("Too low! Try again: ");

                    }
                else {
                    attemptCounter++; //else clause does the opposite of if clause
                    System.out.print("Too high! Try again: ");

                    }


                }
            catch( Exception e){
                    System.out.println("Invalid input");

                    }
            }

    }
    public static void main(String[] args){
        Scanner input = new Scanner (System.in);
        theGame (input);


        System.out.println("Play again? (Y/N)");

        try{
            char answer=input.next().toLowerCase().charAt(0);



            //toLowerCase method so that N =n = no !

            if (answer =='y') theGame (input);


            else if (answer =='n') System.out.println("Good bye");


            input.close(); //no more input data

            }
        catch(Exception e){
            System.out.println("invalid input");
        }
    }


}

so when the user types in the wrong type ie not int it prints out invalid input . This is however not the problem the problem is that it prints that out infinitely. I tried adjusting the try catch blocks but it didnt help at all

nextInt doesnt remove non-integer data from the input buffer so it gets recycled indefinitely unless the data is consumed. In this case an InputMismatchException is thrown by the method so you could write your exception block as

} catch (InputMismatchException e) {
    System.out.println("Invalid input " + input.nextLine());
}

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