Word搜索程序,我做错了什么?



我研究一个单词搜索程序已经有一段时间了。它接受一个文本文件作为输入,例如:

7 //number of rows
15 // number of columns
mucatpoltqfegkq
hfytpnsdlhcorey
pgrhdqsypyscped
gkagdntorioapje
yerjodxnqzztfmf
hypmmgoronkzhuo
qrtzaulhtgtqaao

然后查找用户输入的单词。文件读取和数组创建在一个单独的类中进行。

现在,我需要让它从左到右水平地查找单词,向下,从左上到右下对角线。我要做的是首先找到第一个字母出现的位置,然后从这个位置开始计算单词的其余部分。

我到目前为止所做的只是有时有效。我能够在第一行垂直地找到"猫",但当我试图在对角线上找到披萨时,我得到了一个出界错误。我知道这意味着有些东西超出了数组的范围,我知道如何在更简单的程序中修复它(比如遍历数组的for循环),但在这里不知道。

我还没有开始使用checkDown方法,因为我想弄清楚我现在已经弄清楚的问题。下面是我的代码:

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;
        for (int row = 0; row < inArray.length; row++)
        {
            for (int col = 0; col < inArray[row].length; col++)
            {
                System.out.print(inArray[row][col]);
            }
            System.out.println();
        }
        System.out.println();
    }
    public void play()
    {
        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);
    }
    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();
                }
            }
            System.out.println();
        }
    }
    public void suspectAnalysis()
    {
        checkRight();
        checkDown();
        checkDiagonal();
    }
    public void checkRight()
    {
        for(int i = 1; i < (targetWord.length()); i++)
        {
            if(array[rowLocation][colLocation + i] == targetWord.charAt(i))
            {
                System.out.println(targetWord + " found horizontally at row " + rowLocation + " and column " + colLocation);
            }
        }
    }
    public void checkDown()
    {
        //code goes here
    }
    public void checkDiagonal()
    {
        for(int i = 1; i < (targetWord.length()); i++)
        {
            if(array[rowLocation + i][colLocation + i] == targetWord.charAt(i))
            {
                System.out.println(targetWord + " found diagonally at row " + rowLocation + " and column " + colLocation);
            }
        }
    }
}

我很感激任何帮助。谢谢你!

你的checkDiagonal()方法是去outOfBounds,因为你没有添加一个条件来检查你的[rowLocation+i][colLocation+i]是否在数组的边界内。加上这个条件,你就可以开始了。

上面的评论说:if(array[rowLocation][colLocation + i] == targetWord.charAt(i))看起来很可疑。

如果你的单词沿着网格的右侧垂直对齐会发生什么?您应该考虑在该语句之前添加if语句,以检查[rowLocation + i][colLocation + i]是否在限定范围内。如果没有,则可以确保单词没有以这种方式对齐(无论是在checkRight()还是checkDiagonal()函数中),然后可以退出循环并从函数返回以检查另一个方向。

最新更新