Java:线程中的异常";“主要”;java.lang.ArrayIndexOutOfBoundsException:3位于javaapplication9.Tictactoe.setPlay
当输入>2时,我有一个错误索引超出范围 这是我的密码Java:线程中的异常";“主要”;java.lang.ArrayIndexOutOfBoundsException:3位于javaapplication9.Tictactoe.setPlay,java,indexoutofboundsexception,Java,Indexoutofboundsexception,当输入>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++) {
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;
}
}
专用字符串[][]板;
私有静态最终整数行=3;
私有静态final int COLUMNS=3;
私有字符串regex=“\\s{3}”;
公共无效初始化板(){
对于(int i=0;i
有人对我的代码有好的解决方案吗?在您的
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;
}
}
}
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;
}
}
}
public boolean setPlay(inti,intj,字符串播放器){
如果(i<0 | | i>2 | | j<0 | | j>2){
System.out.println(“无效单元格。请重试”);
返回false;
}否则{
if(board[i][j].匹配项(regex)){
棋盘[i][j]=“玩家+”;
返回true;
}否则{
System.out.println(“单元格已被占用”);
返回false;
}
}
}
如果希望单元格从1开始,以3结束,可以在访问/变异数组之前从方法中的i和j中减去1
例如
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;
}
}
}
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;
}
}
}
public boolean setPlay(inti,intj,字符串播放器){
如果(i<1 | | i>3 | | j<1 | | j>3){
System.out.println(“无效单元格。请重试”);
返回false;
}否则{
if(board[i-1][j-1].匹配项(regex)){
棋盘[i-1][j-1]=“玩家+”;
返回true;
}否则{
System.out.println(“单元格已被占用”);
返回false;
}
}
}
如果您共享“board”变量的堆栈跟踪和数据类型,这将非常有用。有了这些小信息,我们无法帮助您。嗯,您的板很可能大小为3x3(毕竟是Tictatcoe),因此任何大于2的索引都将无效。您可能需要刷新有关数组的知识(对于长度为3的数组,有效索引将为0、1和2)。private String[]board;私有静态最终整数行=3;私有静态final int COLUMNS=3;私有字符串regex=“\\s{3}”代码>这是我想要的变量,当用户输入>2将转到此方法时仍然会出错,我只想使用0-2验证用户输入您现在遇到的错误是什么?