Java:单词搜索程序,我做错了什么?

标签 java arrays for-loop multidimensional-array

我从事单词搜索程序已有一段时间了。它需要一个文本文件作为输入,例如:

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] 在数组范围内。添加此条件,您就可以开始了。

关于Java:单词搜索程序,我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40541492/

相关文章:

javascript - 如何通过索引数组重新排列数组?

用于计算循环执行的 Python 习惯用法

java - 如何使用特定算法设置数组的元素?

java - 如何在android中以编程方式包含相同的布局两次?

java - GridView 加载大图片资源慢

c - 处理包含空格的字符数组

javascript - 更改 JavaScript 中 forEach 循环的每次迭代的颜色?

python - 将所有嵌套列表调整为相同的长度

java - 如果 String 是不可变的,如果 String 常量池只有一个特定值的副本,下面的场景如何给出两个不同的输出

java - 在 Android Studio 中构建 Google Endpoints 后端时出现 403 禁止错误