Java ArrayOutOfBounds测试代码不工作
我在测试arrayOutOfBounds异常时遇到了问题。在下面的代码中,我的if。。。else语句应该可以阻止骑士离开我的棋盘,但我仍然得到例外。有人看到我的错误了吗?感谢您的帮助Java ArrayOutOfBounds测试代码不工作,java,arrays,Java,Arrays,我在测试arrayOutOfBounds异常时遇到了问题。在下面的代码中,我的if。。。else语句应该可以阻止骑士离开我的棋盘,但我仍然得到例外。有人看到我的错误了吗?感谢您的帮助 public int[][] firstMoveChoice() { int knight = 0; x += 1; y += 2; if (x > board.length) { // this tests to make sure the knight does not move off the
public int[][] firstMoveChoice() {
int knight = 0;
x += 1;
y += 2;
if (x > board.length) { // this tests to make sure the knight does not move off the row
System.out.println("Cannot move off board on x axis");
x -= 1;
}
else if (y > board.length) { // this tests to make sure the knight does not move off the column
System.out.println("Cannot move off board on y axis");
y -= 2;
}
else { // this moves the knight when the above statements are false
board[x][y] = ++knight;
System.out.println("This executed");
}
for(int[] row : board) {
printRow(row);
}
}
这是最后一块印刷的电路板:
This executed
1 0 0 0 0 0 0 0
0 0 2 0 0 0 0 0
0 0 0 0 3 0 0 0
0 0 0 0 0 0 4 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at knightstour.Moves.firstMoveChoice(Moves.java:53)
at knightstour.KnightsTour.main(KnightsTour.java:24)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
这是我的棋盘课:
public class ChessBoard {
int[][] board;
public ChessBoard() {
this.board = new int[8][8];
}
}
下面是我的printRow方法:
public static void printRow(int[] row) {
for (int i : row) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
}
这是我的主要方法。当它调用起始位置时,它所做的只是将线路板[0][0]分配给1。如果你想要实际的代码,请告诉我
public static void main(String[] args) {
MoveKnight myKnight = new MoveKnight();
myKnight.startingLocation();
myKnight.firstMoveChoice();
myKnight.firstMoveChoice();
myKnight.firstMoveChoice();
myKnight.firstMoveChoice(); // this moves the knight off the board
}
您的if条件必须为“>=”。尝试:
if (x >= board.length) { // This tests to make sure the knight does not move off the row
System.out.println("Cannot move off board on x axis");
x -= 1;
}
else if (y >= board.length) { // This tests to make sure the knight does not move off the column
System.out.println("Cannot move off board on y axis");
y -= 2;
}
您的if条件必须为“>=”。尝试:
if (x >= board.length) { // This tests to make sure the knight does not move off the row
System.out.println("Cannot move off board on x axis");
x -= 1;
}
else if (y >= board.length) { // This tests to make sure the knight does not move off the column
System.out.println("Cannot move off board on y axis");
y -= 2;
}
我猜您是从0枚举行/列,而length返回数组的真实长度,所以对表单进行测试
if x > board.length
是不正确的,因为只有{0,1,2,3,4,5,6,7}中的x应该是正确的,在您的例子中,8也不大于8。将这些条件更改为
if x >= board.length
这同样适用于y我猜您从0枚举行/列,而length返回数组的真实长度,所以测试形式
if x > board.length
是不正确的,因为只有{0,1,2,3,4,5,6,7}中的x应该是正确的,在您的例子中,8也不大于8。将这些条件更改为
if x >= board.length
同样适用于y>需要>=-在上一次迭代中,y是8,board.length也是8,因此条件不满足>,在任何时候你都没有实例化你的board,你能用你对board对象的声明来显示代码吗?>=表示大于或等于;因此,当您达到设计的最大值时,它会再次循环一次,因为布尔语句是正确的。>需要>=-在上一次迭代中,y是8,board.length也是8,因此条件不满足>如果您没有实例化您的电路板,您能用board对象的声明显示代码吗,请?>=表示大于或等于;因此,当您达到设计的最大值时,它会再次循环一次,因为布尔语句是正确的。非常感谢你!好的,我刚试过,效果不错。非常感谢你!谢谢你向我解释!谢谢你抽出时间!谢谢你向我解释!谢谢你抽出时间!