我正在用java创建一个井字游戏来完成家庭作业。我有一个名为 TicTacToe 的父类和一个名为 humanVsHuman 的派生类。
下面的方法写在派生类中。它提示用户输入他们想要输入棋子的位置(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)
一旦显示输入位置的提示并且在我能够输入位置之前,我就会收到错误消息。
在此先感谢您的帮助。