Java:线程 "main" 中的异常 java.lang.ArrayIndexOutOfBoundsException:3 在 javaapplication9.Tictactoe.setPlay
Java : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at javaapplication9.Tictactoe.setPlay
当输入 > 2 时出现错误索引越界
这是我的代码
private String[][] board;
private static final int ROWS = 3;
private static final int COLUMNS = 3;
private String regex = "\s{3}";
public void initializeBoard() {
for(int i = 0; i < ROWS; i++) {
for(int j = 0; j < COLUMNS; j++) {
board[i][j] = " ";
}
}
}
public boolean setPlay(int i, int j, String player) {
if(board[i][j].matches(regex)) {
board[i][j] = " "+player+" ";
return true;
} else {
System.out.println("The cell is already taken.");
return false;
}
}
有人对我的代码有好的解决方案吗?
在您的 setPlay
方法中,您传递的输入值 i
或 j
的整数值大于 2。您应该只允许从 0 到 2 的输入值。
数组的索引从0开始,到(长度-1)结束。在您的情况下,索引范围是 0 - 2 (0, 1, 2)。您正在尝试调用索引为 3 的方法,该方法抛出 ArrayIndexOutOfBoundException
。在您的方法中执行逻辑之前尝试检查索引范围。
例如
public boolean setPlay(int i, int j, String player) {
if (i < 0 || i > 2 || j < 0 || j > 2) {
System.out.println("Invalid cell. Try again.");
return false;
} else {
if (board[i][j].matches(regex)) {
board[i][j] = " " + player + " ";
return true;
} else {
System.out.println("The cell is already taken.");
return false;
}
}
}
如果您希望您的单元格从 1 开始并以 3 结束,您可以在 accessing/mutating 数组之前的方法中从 i 和 j 中减去 1。
例如
public boolean setPlay(int i, int j, String player) {
if (i < 1 || i > 3 || j < 1 || j > 3) {
System.out.println("Invalid cell. Try again.");
return false;
} else {
if (board[i - 1][j - 1].matches(regex)) {
board[i - 1][j - 1] = " " + player + " ";
return true;
} else {
System.out.println("The cell is already taken.");
return false;
}
}
}
当输入 > 2 时出现错误索引越界
这是我的代码
private String[][] board;
private static final int ROWS = 3;
private static final int COLUMNS = 3;
private String regex = "\s{3}";
public void initializeBoard() {
for(int i = 0; i < ROWS; i++) {
for(int j = 0; j < COLUMNS; j++) {
board[i][j] = " ";
}
}
}
public boolean setPlay(int i, int j, String player) {
if(board[i][j].matches(regex)) {
board[i][j] = " "+player+" ";
return true;
} else {
System.out.println("The cell is already taken.");
return false;
}
}
有人对我的代码有好的解决方案吗?
在您的 setPlay
方法中,您传递的输入值 i
或 j
的整数值大于 2。您应该只允许从 0 到 2 的输入值。
数组的索引从0开始,到(长度-1)结束。在您的情况下,索引范围是 0 - 2 (0, 1, 2)。您正在尝试调用索引为 3 的方法,该方法抛出 ArrayIndexOutOfBoundException
。在您的方法中执行逻辑之前尝试检查索引范围。
例如
public boolean setPlay(int i, int j, String player) {
if (i < 0 || i > 2 || j < 0 || j > 2) {
System.out.println("Invalid cell. Try again.");
return false;
} else {
if (board[i][j].matches(regex)) {
board[i][j] = " " + player + " ";
return true;
} else {
System.out.println("The cell is already taken.");
return false;
}
}
}
如果您希望您的单元格从 1 开始并以 3 结束,您可以在 accessing/mutating 数组之前的方法中从 i 和 j 中减去 1。
例如
public boolean setPlay(int i, int j, String player) {
if (i < 1 || i > 3 || j < 1 || j > 3) {
System.out.println("Invalid cell. Try again.");
return false;
} else {
if (board[i - 1][j - 1].matches(regex)) {
board[i - 1][j - 1] = " " + player + " ";
return true;
} else {
System.out.println("The cell is already taken.");
return false;
}
}
}