【发布时间】:2020-03-21 15:43:44
【问题描述】:
我是一名初学者,正在学习 Java 的超技能课程。我试图在 VS Code 中尝试 this tic tac toe 游戏项目。它工作得很好。但是代码在提交时出错了。
代码:
package tictactoe;
import java.util.*;
public class Main {
public static int movesCommitted = 0;
static String[][][] matrix = {{{" ", "13"}, {" ", "23"}, {" ", "33"}},
{{" ", "12"}, {" ", "22"}, {" ", "32"}},
{{" ", "11"}, {" ", "21"}, {" ", "31"}}};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean drawCounter = true;
board(matrix);
do {
String symbol = (movesCommitted % 2 == 0) ? "X" : "O";
validateAndAppend(symbol, scanner); // Validate for wrong input and filled spots
board(matrix); // Display board
movesCommitted++;
if (anyWinner(symbol)) {
drawCounter = false;
System.out.println(symbol + " wins");
break;
}
} while (movesCommitted < 9);
if (drawCounter) {
System.out.println("Draw");
}
}
public static void board(String[][][] arr) {
System.out.println("---------");
System.out.println("| " + arr[0][0][0] + " " + arr[0][1][0] + " " + arr[0][2][0] + " |");
System.out.println("| " + arr[1][0][0] + " " + arr[1][1][0] + " " + arr[1][2][0] + " |");
System.out.println("| " + arr[2][0][0] + " " + arr[2][1][0] + " " + arr[2][2][0] + " |");
System.out.println("---------");
}
public static void validateAndAppend(String symbol, Scanner scanner) {
boolean cont = true;
do {
System.out.print("Enter coordinates: ");
String inp = scanner.nextLine(); // Input move
int dataInput;
String input = "0" + inp;
input = input.replace(" ", "");
if (Integer.parseInt(input) < 11) {
System.out.println("You should enter numbers!");
continue;
} else {
dataInput = Integer.parseInt(input);
}
int unit = dataInput % 10;
int tens = dataInput / 10;
if (unit < 1 || unit > 3 || tens < 1 || tens > 3) {
System.out.println("Coordinates should be from 1 to 3!");
continue;
// break;
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (matrix[i][j][1].toString().equals(String.valueOf(dataInput))) {
if (matrix[i][j][0] == " ") {
matrix[i][j][0] = symbol;
cont = false;
} else {
System.out.println("This cell is occupied! Choose another one!");
}
}
}
}
} while (cont);
}
public static Boolean anyWinner(String xo) {
if (movesCommitted < 5) {
return false;
}
for (int i = 0; i <= 2; i++) {
if (matrix[i][0][0] == xo && matrix[i][1][0] == xo && matrix[i][2][0] == xo) {
return true; // This is row check
}
if (matrix[0][i][0] == xo && matrix[1][i][0] == xo && matrix[2][i][0] == xo) {
return true; // This is column check
}
}
if (matrix[0][0][0] == xo && matrix[1][1][0] == xo && matrix[2][2][0] == xo) {
return true; // This is "\" diagonal check
}
if (matrix[0][2][0] == xo && matrix[1][1][0] == xo && matrix[2][0][0] == xo) {
return true; // This is "/" check
}
return false;
}
}
错误:
Exception in test #2
Probably your program run out of input (Scanner tried to read more than expected). If you are sure it's not, this type of exception also happens if you created more than one Scanner object (it is preferred to use a single Scanner in program).
java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at tictactoe.Main.validateAndAppend(Main.java:44)
at tictactoe.Main.main(Main.java:16)
Please find below the output of your program during this failed test.
Note that the '>' character indicates the beginning of the input line.
---
---------
| |
| |
| |
---------
Enter coordinates: >4 1
Coordinates should be from 1 to 3!
Enter coordinates: >3 1
This cell is occupied! Choose another one!
Enter coordinates: >4 4
Coordinates should be from 1 to 3!
Enter coordinates: >2 2
This cell is occupied! Choose another one!
原始错误很长(输入)。似乎错误出在 validateAndAppend() 方法中,程序无法区分站点上的错误输入和正确输入。
【问题讨论】:
标签: java