嘿,我正在做一个TicTacToe游戏,它在每个索引中检查“ x”或“ o”的值,并且它一直给我第一个if语句错误java.lang.ArrayIndexOutOfBoundsException:3。任何暗示为什么会这样的帮助?
public boolean checkRowsForWin(int usrBrd, int WinSeq) {
int countX = 0;
int countO = 0;
for(int i = 0; i < usrBrd; i++) {
for(int j = 0; i< usrBrd; j++) {
if(board[i][j] == 'x') {
countX++;
}
else if(board[i][j] == 'o') {
countO++;
}
else {
countX = 0;
countO = 0;
}
if (countX >= WinSeq) {
return true;
}
else if (countO >= WinSeq) {
return true;
}
}
}
return false;
}
在这里我初始化板子
Scanner scnr = new Scanner (System.in);
System.out.println("Tic-Tac-Toe!");
System.out.println("How large would you like the board?(Must be greater
than 3)");
int usrBrd = scnr.nextInt();
if(usrBrd < 3) {
System.out.println("Not a valid number, please try again");
usrBrd = scnr.nextInt();
}
TicTacToe game = new TicTacToe(usrBrd);
game.initializeBoard(usrBrd);
System.out.println("What should the win sequence be? Write in form of
amount i.e \"3\" ");
int winSeq = scnr.nextInt();
这是初始化板子的方法
public TicTacToe(int usrBrd) {
board = new char[usrBrd][usrBrd];
currentPlayerMark = 'x';
initializeBoard(usrBrd);
}
public void initializeBoard(int usrBrd) {
//loop rows
for (int i = 0; i < usrBrd; i++) {
//loop columns
for(int j = 0; j < usrBrd; j++) {
board[i][j] = '-';
}
}
}
答案 0 :(得分:2)
您的错误是您的第二个for循环
for(int j = 0; i< usrBrd; j++)
您始终检查i