我有这个代码,它将搜索单词并在其中查找单词。我得到了一些东西的工作,我累了BINARY我尝试DRAC,它的工作,但对于一些东西,特别是对角线,继续给我outofbounds错误。
import java.util.Scanner;
public class WordSearch {
public static void main(String[] args) {
Scanner kboard = new Scanner(System.in);
char[][] maze = { {'A','P','B','I','N','A','R','Y','D','C','O','C','A','R','D'},
{'M','T','C','O','S','M','A','L','L','E','A','A','T','E','B'},
{'P','O','E','N','A','M','G','I','S','R','L','N','S','I','R'},
{'R','L','D','H','M','A','S','P','I','N','S','T','E','S','T'},
{'A','E','A','E','T','G','T','A','B','M','U','H','A','S','G'},
{'L','M','G','N','R','E','D','O','O','P','B','E','F','E','H'},
{'U','Y','L','O','E','N','O','K','A','L','L','R','A','M','T'},
{'C','I','E','V','E','T','E','C','U','N','A','C','D','Y','I'},
{'R','C','C','A','E','H','S','E','E','W','N','U','E','T','A'},
{'I','I','I','S','O','N','W','D','D','O','O','L','O','H','N'},
{'C','T','N','L','E','H','S','O','H','R','L','E','U','O','J'},
{'I','A','E','P','I','R','A','M','O','C','I','S','T','L','I'},
{'M','N','R','T','A','E','L','D','E','R','S','P','O','O','L'},
{'E','E','E','E','N','R','E','T','T','A','P','A','T','G','I'},
{'S','V','B','L','A','C','K','H','O','L','E','S','O','Y','N'}, };
for(int i = 0; i <= 14; i++)
{
for(int j = 0; j <= 14; j++)
{
System.out.print(maze[i][j] + " ");
}
System.out.println();
}
String input = kboard.nextLine();
//checking the word search horizontally
for(int r = 0; r <= maze.length - 1; r++)
{
for(int c = 0; c <= (maze.length - input.length()); c++)
{
boolean match = true;
for(int i=0; i<input.length(); i++)
{
if(maze[r][c + i] != input.charAt(i))
{
match = false;
break;
}
}
if(match)
{
System.out.print("Found match (horizontal) for " + input + " starting at (" + r + ", " + c + ")");
}
}
}
//checking the word search backwards horizontally
for(int r = 0; r < maze.length - 1; r++)
{
for(int c = 0; c <= (maze.length + 3 - input.length()); c++)
{
boolean match = true;
for(int i = 0; i < input.length(); i++)
{
if(maze[r][c - i] != input.charAt(i))
{
match = false;
break;
}
}
if(match)
{
System.out.print("Found match (backwards horizontal) for " + input + " starting at (" + r + ", " + c + ")");
}
}
}
//checking the word search diagonally down
for(int r = 0; r < maze.length - 1; r++)
{
for(int c = 0; c <= (maze.length - input.length()); c++)
{
boolean match = true;
for(int i = 0; i < input.length(); i++)
{
if(maze[r + i][c + i] != input.charAt(i))
{
match = false;
break;
}
}
if(match)
{
System.out.print("Found match (diagonal down) for " + input + " starting at (" + r + ", " + c + ")");
}
}
}
//searching the word search diagonally up
for(int r = 0; r < maze.length - 1; r++)
{
for(int c = 0; c <= (maze.length - input.length()); c++)
{
boolean match = true;
for(int i = 0; i < input.length(); i++)
{
if(maze[r - i][c - i] != input.charAt(i))
{
match = false;
break;
}
}
if(match)
{
System.out.print("Found match (diagonal up) for " + input + " starting at (" + r + ", " + c + ")");
}
}
}
}
}
我已经尝试过数字并且不断出现错误,例如
Found match (horizontal) for BLACKHOLE starting at (14, 2)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at WordSearch.main(WordSearch.java:112)`
另外,每次使用相同的for循环是不是很糟糕?我只需要更改每个第三个for循环中的一行吗?
答案 0 :(得分:0)
此行导致问题:
if(maze[r - i][c - i] != input.charAt(i))
因为循环只增加i和c,所以r保持为0,所以0 - 1 = -1,因此超出范围。
调整为:
if(maze[r][c - i] != input.charAt(i))
停止越界错误,当搜索“BLACK”时,循环似乎遍历每行的所有列,但不能使对角搜索工作正常。
你不要求采用不同的方法,但你可以尝试从网格上的每个方向向各个方向移动,并捕捉/忽略任何异常异常,因为你知道它们将是错误的结果,只是一个想法。