Java: 2D char数组搜索



我正在做一个单词搜索程序,我想我快要弄清楚了,但是我仍然有一些问题。我的程序读取一个由行和列字母组成的文本文件,并将其转换为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");
        
        
        
    }
}

最新更新