2016-11-12 127 views
1

我正在做一個單詞搜索程序,我想我已經接近了解決它,但我仍然有一些問題。我的程序讀入一個由行和字母組成的文本文件,並將它轉換爲一個2d字符數組到一個單獨的類中。這是我的實際詞搜索類:Java:二維字符數組搜索

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循環,所以它最終會成爲一個無限循環。有時即使一個單詞可以在水平和垂直方向找到,程序也只會說它是水平的。而且,在某些情況下,它會打印出兩次發現該單詞的位置。

任何幫助搞清楚什麼是錯誤將不勝感激。謝謝!

回答

0

它看起來像你的終止字符串是quit,而不是end。另外,它發現錯誤詞語的原因是因爲即使只有一個字符匹配,你也接受了一個目標詞。

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

謝謝你,我改變了我的代碼,現在它檢查某個位置的字符是否等於'targetWord.charAt(i)',並用return語句終止循環。我移動了將找到的單詞的位置打印到for循環外部的語句,所以現在只有在for循環沒有被終止時纔會執行。我將do-while循環的條件改爲'while(!targetWord.equals(「end」));'。 – Bluasul

+0

單獨或一起測試時,正確和對角線方法可以很好地工作,但即使我輸入end並返回一個超出界限的錯誤,'checkDown'方法也會執行。任何想法如何解決這個問題?我會更新我的代碼,以便更好地查看更改 – Bluasul