这是一个单词搜索程序。它搜索的文本被输入并转换为另一个类中的2D数组。
这是该计划正在搜索的文字:
10 //rows
15 //columns
fqexfecmxdvjlgu
cxomfslieyitqtz
nucatfakuxofegk
hfytpnsdlhcorey
pgrhdqsypyscped
ckadhyudtioapje
yerjodxnqzztfmf
hypmmgoronkzhuo
hdskymmpkzokaao
amuewqvtmrlglad
由于某种原因,即使我输入了终止字符串end
,它也会进入我的checkDown()
方法并创建一个越界错误。如果我注释掉该方法并只执行checkRight()
和checkDiagonal()
方法,那么一切似乎都能正常工作。
这是我的代码:
import java.util.Scanner;
public class WordSearch
{
private char[][] array;
private String targetWord;
private int rowLocation;
private int colLocation;
public WordSearch(char[][] inArray)
{
array = inArray;
}
public void play()
{
do{
for (int row = 0; row < array.length; row++)
{
for (int col = 0; col < array[row].length; col++)
{
System.out.print(array[row][col]);
}
System.out.println();
}
System.out.println();
Scanner input = new Scanner(System.in);
System.out.println("What word would you like to search for? Type end to quit: ");
targetWord = input.nextLine();
System.out.println("Typed in: " + targetWord);
System.out.println();
compareFirst(targetWord);
} while (!targetWord.equals("end"));
}
public void compareFirst(String inWord)
{
for (int row = 0; row < array.length; row++)
{
for (int col = 0; col < array[row].length; col++)
{
if(array[row][col] == inWord.charAt(0))
{
rowLocation = row;
colLocation = col;
suspectAnalysis();
}
}
}
}
public void suspectAnalysis()
{
checkRight();
checkDown();
checkDiagonal();
}
public void checkRight()
{
for(int i = 1; i < (targetWord.length()); i++)
{
if(colLocation + i > array[0].length - 1)
{
return;
}
else if(array[rowLocation][colLocation + i] != targetWord.charAt(i))
{
return;
}
}
System.out.println(targetWord + " found horizontally at row " + rowLocation + " and column " + colLocation);
System.out.println();
return;
}
public void checkDown()
{
for(int i = 1; i < (targetWord.length()); i++)
{
if(rowLocation + i > array.length - 1 && colLocation + i > array[0].length - 1)
{
return;
}
else if(array[rowLocation + i][colLocation] != targetWord.charAt(i))
{
return;
}
}
System.out.println(targetWord + " found vertically at row " + rowLocation + " and column " + colLocation);
System.out.println();
}
public void checkDiagonal()
{
for(int i = 1; i < (targetWord.length()); i++)
{
if(colLocation + i > array[0].length - 1 || rowLocation + i > array.length - 1)
{
return;
}
else if(array[rowLocation + i][colLocation + i] != targetWord.charAt(i))
{
return;
}
}
System.out.println(targetWord + " found diagonally at row " + rowLocation + " and column " + colLocation);
System.out.println();
}
}
当我发表checkDown()
方法时,为什么不会发生这种情况?我该如何解决?
我很感激任何帮助。谢谢!
答案 0 :(得分:0)
有三种情况导致失败:
因为你使用了do-while-loop,它在循环结束时检查他的状态,程序正在寻找单词&#34;在结束前结束
你的最后一行有一个&#34; e&#34; (那就是你的运气;-))。所以你的分析开始了 第9行,第3行。
这个条件:
if(rowLocation + i&gt; array.length - 1&amp;&amp; colLocation + i&gt; array [0] .length - 1)
又名if(9 + 1 > 9 && 3 + 1 > 14)
返回true&amp;&amp; false =&gt;假 在下一个条件下导致反弹:
else if(array[rowLocation + i][colLocation] != targetWord.charAt(i))
又名
else if(array[9 + 1][3] != targetWord.charAt(i))
所以改变&amp;&amp;到|| ...
如果你不想查看单词&#34; end&#34;用一段时间(真实)交换你的do-while并通过
检查if(!targetword.equals("end"))
break;
扫描仪后立即。