首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java: 2D字符数组搜索

Java: 2D字符数组搜索
EN

Stack Overflow用户
提问于 2016-11-12 09:18:31
回答 2查看 1.9K关注 0票数 2

我正在做一个单词搜索程序,我想我已经接近解决这个问题了,但是我仍然有一些问题。我的程序读取由字母行和列组成的文本文件,并将其转换为二维字符数组,再转换为一个单独的类。这是我实际的单词搜索类:

代码语言:javascript
复制
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循环,所以它最终只会是一个无限循环。有时,即使一个单词可以在水平方向和垂直方向上找到,程序也只会说它是水平方向找到的。此外,在某些情况下,它会打印出找到单词的位置两次。

任何帮助找出问题所在的人都将不胜感激。谢谢!

EN

回答 2

Stack Overflow用户

发布于 2016-11-12 09:42:17

看起来您的终止字符串是quit,而不是end。此外,它之所以会发现错误的单词,是因为即使只有一个字符匹配,您也会接受targetWord。

代码语言:javascript
复制
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),则自动接受此单词。这是不正确的,因为您必须检查每个位置的所有字母是否匹配。

票数 0
EN

Stack Overflow用户

发布于 2022-01-07 16:42:30

对它进行了一些修改,以处理所有情况。

代码语言:javascript
复制
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");
        
        
        
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40558580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档