[英]Java: 2D char array search
我正在做一个单词搜索程序,我想我已经接近解决了,但我仍然有一些问题。 我的程序读入一个由字母的行和列组成的文本文件,并将其转换为一个 2d 字符数组到一个单独的类中。 这是我的实际单词搜索类:
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();
}
}
因此,它通常设法在所有三个方向上找到单词,但在找到第一个字母和其后的任何其他字母时也会“找到”单词。 它还找到了“end”这个词,它应该终止do-while循环,所以它最终只是一个无限循环。 有时即使一个词可以在水平和垂直方向上找到,程序也只会说它是水平找到的。 此外,在某些情况下,它会打印出两次找到该单词的位置。
任何帮助找出问题所在将不胜感激。 谢谢!
看起来您的终止字符串已quit
,而不是end
。 另外,它找到错误单词的原因是因为即使只有一个字符匹配,您也接受targetWord。
public void checkRight()
{
for(int i = 1; i < (targetWord.length()); i++)
{
if(colLocation + i > array.length - 1)
{
return;
}
else if(array[rowLocation][colLocation + i] == targetWord.charAt(i))
{
System.out.println(targetWord + " found horizontally at row " + rowLocation + " and column " + colLocation);
System.out.println();
}
}
}
也就是说,如果array[rowLocation][colLocation+i] == targetWord.charAt(i)
,那么您将自动接受此单词。 这是不正确的,因为您必须检查所有字母是否在每个位置都匹配。
对其进行了一点修改以处理所有条件。
public class WordSearch
{
public static void compareFirst(char array[][], String targetWord)
{
for (int row = 0; row < array.length; row++)
{
for (int col = 0; col < array[row].length; col++)
{
if(array[row][col] == targetWord.charAt(0))
{
suspectAnalysis(array, row, col, targetWord);
}
}
}
}
public static void suspectAnalysis(char array[][], int rowLocation, int colLocation, String targetWord)
{
checkRight(array, rowLocation, colLocation, targetWord);
checkDown(array, rowLocation, colLocation, targetWord);
checkDiagonal(array, rowLocation, colLocation, targetWord);
}
public static void checkRight(char array[][], int rowLocation, int colLocation, String targetWord)
{
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 static void checkDown(char array[][],int rowLocation, int colLocation, String targetWord)
{
for(int i = 1; i < (targetWord.length()); i++)
{
if(rowLocation + i >= array.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 static void checkDiagonal(char array[][], int rowLocation, int colLocation, String targetWord)
{
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();
}
public static void main(String[] args) {
char array [][] = {
{'D', 'D' , 'D', 'O' , 'G'},
{'C', 'O', 'G', 'O', 'D'},
{'C', 'A', 'G', 'K', 'M'},
{'Z', 'A', 'T', 'Y', 'L'},
{'X', 'C', 'T', 'N', 'N'}
};
compareFirst(array, "DDOG");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.