我在java中为一个家庭作业创建一个tic tac toe游戏。我有一个名为TicTacToe的父类和一个名为humanVsHuman的派生类。我在java中得到这个运行时错误:java.util.NoSuchElementException,我不知道如何修复它
以下方法是在派生类中编写的。它会提示用户输入他们想要输入游戏块(X或O)的位置,然后从父类中调用两个方法:一个将X或O存储在一个名为setGb()的多维数组中,另一个将显示带有新件的板子叫做displayBoard()。
这里是方法:
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());
}
我得到以下运行时错误:
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)
我只要及时收到错误输入位置显示和前我能够输入位置。
在此先感谢您的帮助。
'Deena,'这个词来自哪里? –
我敢打赌:你还有一个新的扫描仪(System.in)在别的地方,并关闭它。关闭一个'Scanner'关闭底层的流。 –
@VishalK:由代码中的'player1'定义。 – Makoto