java - 二维数组单词搜索

标签 java arrays 2d wordsearch

我正在尝试使用用户输入从文本文件中搜索 2D 数组,但到目前为止,我要么最终发现该单词不在 2D 数组中(它确实是),要么最终说无论什么用户输入位于数组中。例如,单词 red 在数组中,但它输出多次,而不是只输出一次,或者多次输出该单词在数组中。我输入的单词文件位于下面,其中前两个数字用于表示二维数组的大小。谁能给我一些关于我做错了什么的提示,因为我被困住了,需要用新的眼光来看待这个问题。

6   6

d e v o l g
r e d p h k
q c h z j c
p o a a f o
v a m m n l
q t f o x b

我的代码如下,如果我需要稍微清理一下代码,请告诉我,因为我确信有些事情可能有点难以理解,因为我已经为此工作了一段时间了。

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;

public class WordSearch {

public static void main(String[] args) {

    char[][] puzzle = null;
    puzzle = fill(puzzle);


    // create scanner for user input to take the word for searching

    @SuppressWarnings("resource")
    Scanner in = new Scanner(System.in);
    System.out.print("Enter the word you wish to search for (limit of four characters): ");
    String wordToFind = in.nextLine();
    play(wordToFind, puzzle);
    printPuzzle(puzzle);
}

public static char[][] fill(char[][] puzzle) {

    // file Reading

    boolean flag = true;// boolean flag is used for prompting user if the file location is incorrect
    @SuppressWarnings("resource")
    Scanner in = new Scanner(System.in);
    while (flag) {
        System.out.print("Please input the text file locaiton: ");

        String fileLoc = in.nextLine();

        try {

            File f = new File(fileLoc);
            @SuppressWarnings("resource")
            Scanner fileRead = new Scanner(f);

            int row = fileRead.nextInt();// i
            int col = fileRead.nextInt();// j

            puzzle = new char[row][col];

            for (int i = 0; i < puzzle.length; i++) {
                for (int j = 0; j < puzzle[0].length; j++) {
                    puzzle[i][j] = fileRead.next().charAt(0);

                }
            }

            flag = false;// breaks the loop so the user isn't re-prompt for
                            // file location
        } catch (FileNotFoundException e) {

        }

    }
    return puzzle;
}

public static void printPuzzle(char[][] puzzle) {
    for (int i = 0; i < puzzle.length; i++) {
        for (int j = 0; j < puzzle[0].length; j++) {
            System.out.print(puzzle[i][j] + " ");
        }
        System.out.println();
    }
}

public static void play(String word, char[][] puzzle) {
    for (int i = 0; i < puzzle.length; i++) {
        for (int j = 0; j < puzzle[0].length; j++) {

            if (checkUp(puzzle, word, i, j) == true) 
            {
                System.out.println("The word " + word + " was found by the method checkUp beginnning in cell ");


            }
             if ( checkDown(puzzle, word, i, j) == true)
            {
                System.out.println("The word " + word + " was found by the method checkDown beginning in cell");

            }
             if(checkRight(puzzle, word, i, j) == true)
            {
                System.out.println("The word " + word + " was found by the method checkDown beginning in cell");

            }
             if(checkLeft(puzzle, word, i, j) == true)
            {
                System.out.println("The word " + word + " was found by the method checkLeft beginning in cell");

            }

            if(checkUp(puzzle, word, i, j) != true && checkDown(puzzle, word, i, j) != true && checkRight(puzzle, word, i, j) != true && checkLeft(puzzle, word, i, j) != true) 
            {
                System.out.println("The word " + word + " was not in the puzzle");
                break;

            }


        }
    }
}
/**
 * searches for the user defined word going up
 * @param puzzle
 * @param word
 * @param i
 * @param j
 * @return
 */
public static boolean checkUp(char[][] puzzle, String word, int i, int j) {
    char search = word.charAt(0);
    for ( i = 0; i < puzzle.length; i++) {
        for ( j = 0; j < puzzle[0].length; j++) {
            if (search != puzzle[i][j]) {
                return false;

            }

            else {
                i = i - 1;
                if (i < 0) {
                    return false;
                }
            }
        }

    }
    return true;
}



/**
 * searches for the user defined word going down
 * @param puzzle
 * @param word
 * @param i
 * @param j
 * @return
 */
public static boolean checkDown(char[][] puzzle, String word, int i, int j) {
    char search = word.charAt(0);
    for ( i = 0; i < puzzle.length; i++) {
        for ( j = 0; j < puzzle[0].length; j++) {
            if (search != puzzle[i][j]) {
                return false;

            }

            else {
                i = i + 1;
                if (i < 0) {
                    return false;
                }
            }
        }

    }
    return true;
}

/**
 * searches for the user defined word going left
 * @param puzzle
 * @param word
 * @param i
 * @param j
 * @return
 */
public static boolean checkLeft(char[][] puzzle, String word, int i, int j) {
    char search = word.charAt(0);
    for ( i = 0; i < puzzle.length; i++) {
        for ( j = 0; j < puzzle[0].length; j++) {
            if (search != puzzle[i][j]) {
                return false;

            }

            else {
                j = j - 1;
                if (j < 0) {
                    return false;
                }
            }
        }

    }
    return true;
}

/**
 * this method will return true if the user defined word is found going right
 * @param puzzle
 * @param word
 * @param i
 * @param j
 * @return 
 */
public static boolean checkRight(char[][] puzzle, String word, int i, int j) {
    char search = word.charAt(0);
    for ( i = 0; i < puzzle.length; i++) {
        for ( j = 0; j < puzzle[0].length; j++) {
            if (search != puzzle[i][j]) {
                return false;

            }

            else {
                j = j + 1;
                if (j < 0) {
                    return false;
                }
            }
        }

    }
    return true;
}

}

最佳答案

if 语句中放置一个 return; 语句,这样它在找到匹配项后就不会继续迭代。循环结束后可以显示没有找到。

public static void play(String word, char[][] puzzle) {
    String foundMessage = "The word %s was found by the method %s beginnning in cell%n";
    for (int i = 0; i < puzzle.length; i++) {
        for (int j = 0; j < puzzle[0].length; j++) {
            if (checkUp(puzzle, word, i, j)) {
                System.out.printf(foundMessage, word, "checkUp");
                return;
            } else if (checkDown(puzzle, word, i, j)) {
                System.out.printf(foundMessage, word, "checkDown");
                return;
            } else if (checkRight(puzzle, word, i, j)) {
                System.out.printf(foundMessage, word, "checkRight");
                return;
            } else if (checkLeft(puzzle, word, i, j)) {
                System.out.printf(foundMessage, word, "checkLeft");
                return;
            }
        }
    }
    System.out.println("The word " + word + " was not in the puzzle");
}

要检查一个单词是否在二维数组中,您只需要一个循环。例如,checkUp:

public static boolean checkUp(char[][] puzzle, String word, int i, int j) {
    if (i - word.length() >= 0) {
        for (int offset = 0; offset < word.length(); offset++) {
            if (puzzle[i - offset][j] != word.charAt(offset)) {
                return false;
            }
        }
        return true;
    } else {
        return false;
    }
}    

关于java - 二维数组单词搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40857881/

相关文章:

java - JVM 崩溃时异常中显示奇怪的路径

Java Socket : java.net.SocketTimeoutException:读取超时

javascript - 从第一个数组中删除重复的索引值,按照第一个操作第二个(在某些特定条件下)

java - 如何通过输入更改二维数组?

android - 适用于 Android 和 iOS 的 2D 跨平台游戏引擎?

java - 如何使用blueimp jQuery文件上传: Empty file upload result in Struts 2,文件项为空

javascript - 拼接以删除重复的数组值不起作用 javascript

android - 我如何处理数组索引越界?

c - C 中的二维动态数组 : Which of those 3 snippets gets executed faster?

java - 如何读取文本文件并使用它来填充对象数组的数据?