简体   繁体   English

我在Java中遇到此运行时错误:java.util.NoSuchElementException,并且我不确定如何解决它

[英]I am getting this runtime error in java: java.util.NoSuchElementException, and I'm not sure how to fix it

I am creating a tic tac toe game in java for a homework assignment. 我正在用Java创建井字游戏来完成作业。 I have a parent class called TicTacToe and a derived class called humanVsHuman. 我有一个名为TicTacToe的父类和一个名为humanVsHuman的派生类。

The following method is written in the derived class. 在派生类中编写了以下方法。 It prompts the user to enter the position that they want to enter their game piece (X or O) and then calls two methods from the parent class: One that stores the X or O in a multidemensional array called setGb() and one that displays the board with the new piece called displayBoard(). 它提示用户输入他们想要输入游戏作品的位置(X或O),然后从父类中调用两种方法:一种将X或O存储在名为setGb()的多维数组中,另一种则显示带有名为displayBoard()的新零件的面板。

Here is the method: 方法如下:

private void playGame() {
    Scanner keyboard = new Scanner (System.in);
    int row, col;
    System.out.println("When playing, enter the row and column position for your X or O piece separated by a space.");
    do{
        System.out.print(player1 + ", Enter X position: ");
        row = keyboard.nextInt();
        col = keyboard.nextInt();
        setGb(row, col, 'X');
        displayBoard();

        System.out.print(player2 + ", Enter O position: ");
        row = keyboard.nextInt();
        col = keyboard.nextInt();
        setGb (row, col, 'O');
        displayBoard();
        keyboard.close();
    } while (!gameOver());
}

I am getting the following runtime error: 我收到以下运行时错误:

When playing, enter the row and column position for your X or O piece separated by a space.
Deena, Enter X position: Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at Assignment7C.HumanVsHuman.playGame(HumanVsHuman.java:38)
    at Assignment7C.HumanVsHuman.repeatGame(HumanVsHuman.java:28)
    at Assignment7C.HumanVsHuman.game(HumanVsHuman.java:14)
    at Assignment7C.TicTacToeTest.main(TicTacToeTest.java:10)

I'm getting the error as soon as the prompt to enter the position is displayed and before I am able to input the position. 在显示输入位置提示时,以及在我能够输入位置之前,我都会收到错误消息。

Thanks in advance for your help. 在此先感谢您的帮助。

Move keyboard.close() out of the loop. keyboard.close()移出循环。 You don't want to close the Scanner until the game is over. 在游戏结束之前,您不希望关闭Scanner This will not resolve the exception in your problem, but you need to do it anyway to avoid the very next problem you will face. 这不会解决您的问题中的异常,但是无论如何您都需要这样做,以避免您将要面对的下一个问题。

Did you try removing the following code from your while loop and see if it works. 您是否尝试从while循环中删除以下代码,然后查看其是否有效。

    keyboard.close();

Or you can add it outside the loop. 或者,您可以将其添加到循环之外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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