簡體   English   中英

循環無法處理所有內容

[英]Loops aren't processing everything

所以我正在編寫一個填字游戲解謎器,除1個故障外,我幾乎有98%的代碼可以工作。 我有一個循環,該循環從文件中讀取1個單詞,然后將該字符串與chars數組進行比較,以查找該字符串在數組中的位置。 到目前為止,循環讀取了所有單詞並找到了它們的位置,但是我的問題是它跳過了我列表中的3個單詞。 我知道這些單詞在char數組中,但是由於某種原因,它們沒有被拾取。 有任何想法嗎?

這是我的代碼:

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


    public class Test {

        public static void main(String[] args) throws Exception{
            File file = new File("puzzle.txt"); 
            Scanner sc = new Scanner(file);
            int row = sc.nextInt();
            int col = sc.nextInt();
            sc.nextLine();

            //Make an array to hold the puzzle
            char[][] puzzle = new char[row][col];   


             //Read in the strings from the file into the array.
            for (int i=0; i<row; i++){
                String getChar = (new String(sc.next()));
                for(int j = 0;j<col; j++){
                    puzzle[i][j] = getChar.charAt(j);
                    }
                }

            //Read the number of words and move to the next line    
            int numwords = sc.nextInt();
            sc.nextLine();  


            //look for each word
            for(int i=0; i<numwords; i++){
                String word = new String();
                word = sc.nextLine();
                System.out.printf("This is word: %s\n", word);

                //arrays to hold the direction.
                int [] movx ={-1, -1, -1, 0, 0, 1, 1, 1};
                int [] movy ={-1, 0, 1, -1, 1, -1, 0, 1};

                //this variable will hold if we found or not the string in the puzzle
                boolean found = false;

                //find the words in the puzzle
                for(int m = 0; m < puzzle.length; m++) {
                    for(int n = 0; n < puzzle[0].length; n++) {
                        if(puzzle[m][n] == word.charAt(0)){
                            for (int o = 0; o < 8; o++){
                                if(check(m, n, word, puzzle, movx[o], movy[o])){
                                    System.out.printf("%s found at position (%d, %d)\n\n", word, m, n);
                                    found = true;
                                    break;
                                    }
                            }

                        }
                    }
                }
                if (!found){
                    System.out.printf("%s was not found\n\n", word);
                }
            }

        //Close the scanner     
            sc.close();         
        }   
        //This is your generic-direction function
        public static boolean check(int row, int col, String word, char[][] puzzle, int offsetx, int offsety){

            //start with the current position
            int x = row;
            int y = col;

            for (int i = 0; i < word.length(); i++){
                char c = word.charAt(i);

                //Is not equal
                if (puzzle[x][y] != c) return false;

                x += offsetx;
                y += offsety;

                //check the boundaries, if we go out then we didn't find the word;
                if (x < 0 || x >= puzzle.length || y < 0 || y >= puzzle[x].length) return false;
            }

            return true;
        }

    }

(編輯區)

根據我對打印語句的診斷,當我分別為3、8和9時,wordsearch找不到該單詞。 我相信是在這個循環之后:

    //find the words in the puzzle
for(int m = 0; m < puzzle.length; m++)

因為在此循環之前,我的上一個循環遍歷了每個單詞。 單詞被傳遞到該循環中的循環中。 因此,它一直貫穿我所有檢查通過數組中的每個點,然后即使錯誤仍在傳遞,也只是作為假傳遞。

(/編輯區域)

在此循環之前,我測試了

我正在讀取的文件:

    10 10
    WVERTICALL
    ROOAFFLSAB
    ACRILIATOA
    NDODKONWDC
    DRKESOODDK
    OEEPZEGLIW
    MSIIHOAERA
    ALRKRRIRER
    KODIDEDRCD
    HELWSLEUTH
    10
    WEEK
    FIND
    RANDOM
    SLEUTH
    BACKWARD
    VERTICAL
    DIAGONAL
    WIKIPEDIA
    HORIZONTAL
    WORDSEARCH

這是它打印的內容(WEEK不在拼圖中):

    This is word: WEEK and this is i 0
    WEEK was not found 

    This is word: FIND and this is i 1
    FIND found at position (1, 4)

    This is word: RANDOM and this is i 2
    RANDOM found at position (1, 0)

    This is word: SLEUTH and this is i 3
    SLEUTH was not found 

    This is word: BACKWARD and this is i 4
    BACKWARD found at position (1, 9)

    This is word: VERTICAL and this is i 5
    VERTICAL found at position (0, 1)

    This is word: DIAGONAL and this is i 6
    DIAGONAL found at position (8, 6)

    This is word: WIKIPEDIA and this is i 7
    WIKIPEDIA found at position (9, 3)

    This is word: HORIZONTAL and this is i 8
    HORIZONTAL was not found 

    This is word: WORDSEARCH and this is i 9
    WORDSEARCH was not found

這些是它找不到的詞:

    SLEUTH is at position (9,4) 
    HORIZONTAL is at position (9,0)
    WORDSEARCH is at position (0,0)

任何提示,技巧或想法都非常感謝!

在您的檢查方法中:

//check the boundaries, if we go out then we didn't find the word;
if (x < 0 || x >= puzzle.length || y < 0 || y >= puzzle[x].length) return false;

正在檢查您增加x,y值后是否超出了拼圖邊界。 問題在於找不到的三個單詞,找到最后一個字母,增加計數器,然后確定您已經走出難題的邊緣並返回false,即使它已經找到了最后一個字母。

嘗試將條件移至循環的開頭,如下所示:

for (int i = 0; i < word.length(); i++){
    //check the boundaries, if we go out then we didn't find the word;
    if (x < 0 || x >= puzzle.length || y < 0 || y >= puzzle[x].length) return false;

    char c = word.charAt(i);

    //Is not equal
    if (puzzle[x][y] != c) return false;

    x += offsetx;
    y += offsety;
} 

缺少的是您的check方法中的條件。 當單詞的最后一個字符在某個邊緣時,它會失敗,因為它試圖查找下一個鄰居(綁定檢查)。 if (i == word.length() - 1) return true;添加此條件if (i == word.length() - 1) return true; if (puzzle[x][y] != c) return false;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM