Java 为什么我的程序无休止地打印异常语句,给用户错误的输入?

Java 为什么我的程序无休止地打印异常语句,给用户错误的输入?,java,exception,exception-handling,Java,Exception,Exception Handling,我不知道我的标题是否有意义,但下面是代码( 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 inclusi

我不知道我的标题是否有意义,但下面是代码(

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");
        }
    }


}
import java.util.InputMismatchException;
导入java.util.Scanner;
公共类喷涌{
公共静态无效游戏(扫描仪输入){
int randomNum=(int)(Math.random()*101);//对0-100之间的数字进行随机化,包括两者
System.out.println(randomNum);//用于调试目的
int attemptCounter=0;//统计用户的尝试次数
System.out.print(“欢迎参加猜数字游戏!输入您的猜测:”);
while(true){
System.out.println(“这里输入错误”);
试一试{
System.out.println(“这是错误输入之后的内容”);
int userInput=input.nextInt();
if(userInput==randomNum)//当usr输入和生成的随机数相等时,我们打印尝试次数
{
尝试计数器++;
System.out.println(“恭喜你在“+attemptCounter+”尝试之后做出了正确的猜测!”);
打破
}

如果(userInput
nextInt
没有从输入缓冲区中删除非整数数据,因此除非数据被消耗,否则它将被无限期地回收。在这种情况下,该方法会抛出一个
InputMismatchException
,以便您可以将异常块写入

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

你需要对捕获做些什么。一个选择是打破循环。我认为你的代码工作正常。你到底遇到了什么错误?谢谢你的代码使我的代码工作得很好。不过我想请你解释一下你是如何想的,我的意思是你是如何想出这个的?:)所有这些信息都可以使用:如果下一个标记无法转换为有效的int值,则此方法将抛出InputMismatchException