Java:2D字符数组搜索

Java:2D字符数组搜索,java,arrays,loops,multidimensional-array,Java,Arrays,Loops,Multidimensional Array,我正在做一个单词搜索程序,我想我很快就能找到答案,但我仍然有一些问题。我的程序读入一个由字母的行和列组成的文本文件,并将其转换成一个2d字符数组,再转换成一个单独的类。这是我实际的单词搜索类: import java.util.Scanner; public class WordSearch { private char[][] array; private String targetWord; private int rowLocation; private

我正在做一个单词搜索程序,我想我很快就能找到答案,但我仍然有一些问题。我的程序读入一个由字母的行和列组成的文本文件,并将其转换成一个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();
    }
}
import java.util.Scanner;
公共类词汇搜索
{
私有字符[][]数组;
专用字符串targetWord;
私人位置;
私人智力搭配;
公共词搜索(char[][]inArray)
{
数组=数组;
}
公共游戏
{
做{
for(int row=0;row数组[0]。长度-1)
{
返回;
}
else if(数组[rowLocation][并置+i]!=targetWord.charAt(i))
{
返回;
}
}
System.out.println(targetWord+“在行“+行位置+”和列“+并置处水平找到);
System.out.println();
返回;
}
公共无效检查()
{
对于(int i=1;i<(targetWord.length());i++)
{
if(rowLocation+i>array.length-1&&colLocation+i>array[0].length-1)
{
返回;
}
else if(数组[行位置+i][并置]!=targetWord.charAt(i))
{
返回;
}
}
System.out.println(targetWord+“垂直位于行“+行位置+”和列“+并置处);
System.out.println();
}
公共无效检查()
{
对于(int i=1;i<(targetWord.length());i++)
{
if(并置+i>array[0].length-1 | | rowLocation+i>array.length-1)
{
返回;
}
else if(数组[rowLocation+i][consolution+i]!=targetWord.charAt(i))
{
返回;
}
}
System.out.println(targetWord+“位于行“+行位置+”和列“+并置的对角位置);
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();
        }
    }

}
public void checkRight()
{
对于(int i=1;i<(targetWord.length());i++)
{
if(搭配+i>array.length-1)
{
返回;
}
else if(数组[rowLocation][并置+i]==targetWord.charAt(i))
{
System.out.println(targetWord+“在行“+行位置+”和列“+并置处水平找到);
System.out.println();
}
}
}

也就是说,如果
array[rowLocation][colLocation+i]==targetWord.charAt(i)
,那么您会自动接受这个单词。这是不正确的,因为您必须检查每个位置的所有字母是否匹配。

谢谢,我更改了代码,现在它检查特定位置的字符是否不等于
targetWord.charAt(I)
,并用返回语句终止循环。我将打印找到的字的位置的语句移动到for循环的外部,因此它现在仅在for循环未终止时执行。我将do while循环的条件更改为
while(!targetWord.equals(“end”)。单独或一起测试时,right和diagonal方法都能完美工作,但即使我键入end并返回越界错误,“checkDown”方法仍在执行。你知道怎么解决这个问题吗?我将更新我的代码,以便您可以更好地查看更改