即使第一个输入为int,循环中的nextInt()也会在第一个不匹配后无限引发InputMismatchException

我正在制作一个Java控制台游戏,用户可以在一定时间内回答尽可能多的数学问题。如果用户输入始终为int,则游戏运行良好,但我尝试处理输入为其他内容的情况。如果输入不是int,我希望循环继续并询问另一个问题。这是我的游戏线程中run方法的代码:

public void run() {
        try {

            score = 0;
            System.out.println("Go!");
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            Scanner scan = new Scanner(reader);
            while(!Thread.currentThread().isInterrupted()) {
                String[] set = getMathProblem();
                String problem = set[0];
                int answer = Integer.parseInt(set[1]);
                System.out.printf(problem + "%n");
                int guess = answer - 1;
                try {
                    while(!reader.ready()) {
                        Thread.sleep(100);
                    }
                    guess = scan.nextInt();
                    if(Thread.currentThread().isInterrupted()) {
                        scan.close();
                        throw new InterruptedException();
                    }
                } catch(InputMismatchException e) {
                    //executes when this exception occurs
                    System.out.println("Input has to be a number. ");
                    continue;
                } catch (InterruptedException e) {
                    break;
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                if(guess == answer) {
                    System.out.println("Right!");
                    score++;
                } else {
                    System.out.println("Wrong! The answer was: " + answer);
                }

            }
            scan.close();
            throw new InterruptedException();
        } 
        catch(InterruptedException e) 
        {
            System.out.println("Times Up!");
            System.out.println("You got " + score + " problems correct!");
        }
    }

这是我的主要方法的代码,如果有帮助的话:

public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
                System.out.println("Hello! In this game you will have 30 seconds to answer as many math problems as you can");
                System.out.println("Ready to play? (yes/no)");
                Scanner initial = new Scanner(System.in);
                String ready = initial.next();
                if (ready.equals("yes")) {
                    Runnable r = new Game();
                    Thread t1 = new Thread(r);

                    t1.start();

                    Thread.sleep(30000);

                    t1.interrupt();
                    t1.join();
                }
                initial.close();
                System.out.println("Bye!");


    }

我正在写的错误发生在第一个输入是非整数,然后下一个输入是整数的时候。这应该可以正常工作,但是发生这种情况时代码将进入无限循环。

这是输出示例:

Hello! In this game you will have 30 seconds to answer as many math problems as you can
Ready to play? (yes/no)
yes
Go!
0 + 8
j
Input has to be a number. 
8 * 6
3
Input has to be a number. // output starts infinitely here
11 * 14
Input has to be a number. 
8 + 14
Input has to be a number. 
12 * 2
Input has to be a number. 
12 - 5
Input has to be a number. 
...

这是仅带有整数的输出示例:

Hello! In this game you will have 30 seconds to answer as many math problems as you can
Ready to play? (yes/no)
yes
Go!
13 * 1
13
Right!
10 * 18
181
Wrong! The answer was: 180
15 - 8
Times Up!
You got 1 problems correct!
Bye!

I am not sure how to fix this bug, but I think it's happening either because nextInt is still picking up the char from the last input, or because it is somehow taking the problem variable as its input. Please let me know how I can fix this.

评论