作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
检查并保持程序入站的方法不起作用我不断收到:array IndexOutOfBounds 错误
。由于此错误,我无法继续查看我的 Knight's Tour 实际实现是否有效。
import java.util.Arrays;
class KnightsTour {
static int the_board[][] = new int[8][8];
int the_tour[][] = new int [8][8];
int k,moves = 0;
int x = 0, y = 0;
int z, j = 1;
boolean tour_found, isSafe;
//fills 2D array with 0's
public KnightsTour()
{
for (int i = 0; i < 8; i++)
{
for (int r = 0; r < 8; r++)
{
the_board[i][r] = 0;
}
}
}
/*recursive method, checks how many moves were made if 16 were made tour finished,
else if not moves knight checks if the move is valid if not back tracks*/
public void findTour(int q)
{
if(moves == 68)
{
tour_found = true;
}
else move(q);
if(isSafe == true)
{
findTour(q++);
moves++;
}
else
if(isSafe == false)
{
findTour(q*(-1));
moves--;
}
}
//used to keep prevent arrayindexoutofbounds error
public boolean arrayInBounds(int x, int y)
{
if(x > 8 || y > 8)
{
return false;
}
else return true;
}
/*move method uses switch statement to decide which move the knight should make
based on a case number, negative case numbers back track knight. if statement checks
if the current array element is empty and the index is inbounds*/
public void move(int a)
{
switch (a)
{
case 1:
if(arrayInBounds(x+2, y+1) == true){
if(the_board[x+2][y+1] != 0){
the_board[x+2][y+1]=j;
j++;
}
}
else isSafe = false;
case 2:
if (arrayInBounds(x+1, y+2) == true){
if(the_board[x+1][y+2] != 0){
the_board[x+1][y+2]=j;
j++;
}
}
else isSafe = false;
case 3:
if(arrayInBounds(x-1, y+2) == true){
if(the_board[x-1][y+2] != 0){
the_board[x-1][y+2]=j;
j++;
}
}
else isSafe = false;
case 4:
if (arrayInBounds(x-2, y+1) == true){
if(the_board[x-2][y+1] != 0){
the_board[x-2][y+1]=j;
j++;
}
}
else isSafe = false;
case 5:
if(arrayInBounds(x-2, y-1) == true){
if(the_board[x-2][y-1] != 0){
the_board[x-2][y-1]=j;
j++;
}
}
else isSafe = false;
case 6:
if(arrayInBounds(x-1, y-2) == true){
if(the_board[x-1][y-2] != 0){
the_board[x-1][y-2]=j;
j++;
}
}
else isSafe = false;
case 7:
if(arrayInBounds(x+1, y-2) == true){
if(the_board[x+1][y-2] != 0){
the_board[x+1][y-2]=j;
j++;
}
}
else isSafe = false;
case 8:
if(arrayInBounds(x+2, y-1) == true){
if(the_board[x+2][y-1] != 0){
the_board[x+2][y-1]=j;
j++;
}
}
else isSafe = false;
case -1:
the_board[x-2][y-1]=0;
j--;
case -2:
the_board[x-1][y-2]=0;
j--;
case -3:
the_board[x+1][y-2]=0;
j--;
case -4:
the_board[x+2][y-1]=0;
j--;
case -5:
the_board[x+2][y+1]=0;
j--;
case -6:
the_board[x+1][y+2]=0;
j--;
case -7:
the_board[x-1][y+2]=0;
j--;
case -8:
the_board[x-2][y+1]=0;
j--;
}
}
//for loop to display tour once found//
public void displayTour()
{
int v = 1;
for (int i = 0; i < 8; i++)
{
for (int e = 0; e < 8; e++)
{
if(v % 8 == 0)
{
System.out.print(the_board[i][e] + "\t");
System.out.println("\n");
}
else
System.out.print(the_board[i][e] + "\t");
v++;
}
}
}
}
最佳答案
您的arrayInBounds
方法不正确。 8
是无效索引,但您的方法会错误地将其报告为有效。此外,它不会检查 0
以下的无效索引。
更改方法以检查 x
或 y
是否大于 或等于 8
,并且还要检查其中一个是否小于 0
。
关于java - 尽管使用 indexInBounds 仍获取 ArrayIndexOutOfBounds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21996728/
检查并保持程序入站的方法不起作用我不断收到:array IndexOutOfBounds 错误。由于此错误,我无法继续查看我的 Knight's Tour 实际实现是否有效。 import java.
我是一名优秀的程序员,十分优秀!