2016-11-12 98 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