2015-10-03 71 views
2

我有一個代碼,用於檢查一個wordpuzzle是否包含某個單詞。 這是識別TestClass:爲什麼此代碼返回false而不是true?

package application; 

import org.junit.Before; 
import org.junit.Test; 
import static org.junit.Assert.*; 

/** 
* Test class for the implementation of the {@link WordPuzzle} 
* 
*/ 
public class WordPuzzleTest { 

    WordPuzzle myPuzzle = null; 

    /** 
    * This function will initialize the myPuzzle variable before you start a new test method 
    * @throws Exception 
    */ 
    @Before 
    public void setUp() { 
     try { 
      this.myPuzzle = new WordPuzzle("VNYBKGSRORANGEETRNXWPLAEALKAPMHNWMRPOCAXBGATNOMEL", 7); 
     } catch (IllegalArgumentException ex) { 
       System.out.println("An exception has occured"); 
       System.out.println(ex.getMessage()); 
     } 

    } 

    /** 
    * Test the constructor of the {@link WordPuzzle} class 
    */ 
    @Test 
    public void testWordPuzzle() { 
      assertNotNull("The object failed to initialize", this.myPuzzle); 
      char[][] expectedArray = {{'V','N','Y','B','K','G','S'}, 
           {'R','O','R','A','N','G','E'}, 
           {'E','T','R','N','X','W','P'}, 
           {'L','A','E','A','L','K','A'}, 
           {'P','M','H','N','W','M','R'}, 
           {'P','O','C','A','X','B','G'}, 
           {'A','T','N','O','M','E','L'}}; 
      assertArrayEquals(expectedArray, this.myPuzzle.getLetterArray()); 
    } 

    /** 
    * Test to search for some words... 
    */ 
    @Test 
    public void testSearchWord() { 
      assertFalse("The word SOFTWARE is found, and may not be found", this.myPuzzle.searchWord("SOFTWARE")); 
      assertTrue("The word BANANA is not found", this.myPuzzle.searchWord("BANANA")); 
    } 

} 

這是我寫的發現,某些字的代碼:

package application; 

public class WordPuzzle { 

    private String puzzle; 
    private int numRows; 
    private char [][] puzzleArray = new char[numRows][numRows]; 

    public WordPuzzle(String puzzle, int numRows) { 
     super(); 
     this.puzzle = puzzle; 
     this.numRows = numRows; 

     puzzleArray = new char[numRows][numRows]; 
     char[] puzzleChar; 
     puzzleChar=puzzle.toCharArray(); 

     int index=0; 
     int i=0; 
     int j=0; 
     while (i<numRows) { 
      while (j<numRows) { 
       puzzleArray[i][j] = puzzleChar[index]; 
       j++; 
       index++; 
      } 
      i++; 
      j=0; 
     } 
    } 

    public Object[] getLetterArray() { 
     return puzzleArray; 
    } 

    public boolean searchWord(String word) { 
     char[] wordChar; 
     wordChar = new char[word.length()]; 
     int xaxis=0; 
     int yaxis=0; 
     int index=0; 
     boolean wordFound=false; 

     while (yaxis<numRows) { 
      while (xaxis<numRows) { 
       /** 
       * Find first matching letter 
       */ 
       if (puzzleArray[yaxis][xaxis]!=wordChar[index]) { 
        xaxis++; 
       } 
       else { 
        int xinit=xaxis; 
        int yinit=yaxis; 

        /** 
        * Check left 
        */ 
        while (xaxis>-1 && puzzleArray[yaxis][xaxis]==wordChar[index]) { 
         if (index==word.length()-1) { 
          wordFound=true; 
          return wordFound; 
         } 
         else { 
          xaxis--; 
          index++; 
         } 
        } 
        xaxis=xinit; 
        yaxis=yinit; 
        index=0; 

        /** 
        * Check up 
        */ 
        while (yaxis>-1 && puzzleArray[yaxis][xaxis]==wordChar[index]) { 
         if (index==word.length()-1) { 
          wordFound=true; 
          return wordFound; 
         } 
         else { 
          yaxis--; 
          index++; 
         } 
        } 
        xaxis=xinit; 
        yaxis=yinit; 
        index=0; 

        /** 
        * Check down 
        */ 
        while (yaxis<numRows && puzzleArray[yaxis][xaxis]==wordChar[index]) { 
         if (index==word.length()-1) { 
          wordFound=true; 
          return wordFound; 
         } 
         else { 
          yaxis++; 
          index++; 
         } 
        } 
        xaxis=xinit; 
        yaxis=yinit; 
        index=0; 

        /** 
        * Check right 
        */ 
        while (xaxis<numRows && puzzleArray[yaxis][xaxis]==wordChar[index]) { 
         if (index==word.length()-1) { 
          wordFound=true; 
          return wordFound; 
         } 
         else { 
          xaxis++; 
          index++; 
         } 
        } 
        xaxis=xinit; 
        yaxis=yinit; 
       } 
      } 
      yaxis++; 
      xaxis=0; 
     } 
    return wordFound; 
    } 
} 

當我運行測試,它會說,它並沒有發現「香蕉」。我找不到我的錯誤。它應該返回

wordFound=true 

向下檢查時。但它不...

+7

更換 wordChar = new char[word.length()]; 您可以使用調試器和檢查程序的執行流程。 –

回答

3

您不用輸入字符串初始化char數組。

變化

public boolean searchWord(String word) { 
    char[] wordChar; 
    wordChar = new char[word.length()]; 
    ... 

public boolean searchWord(String word) { 
    char[] wordChar = word.toCharArray(); 
    ... 
0

在搜索內容的方法(在WordPuzzle類),使用的是字符數組:

wordChar = new char[word.length()]; 

但是你從來沒有設置你的字符串變量「單詞」的值(你剛剛給它的長度)。

然後,這一點,如果條件:

if (puzzleArray[yaxis][xaxis]!=wordChar[index]) { 
       xaxis++; 
      } 

始終是真實的,你的方法只是遞增變量並檢查這個沒用的條件,並用虛假申報完成。

嘗試

wordChar = word.toCharArray(); 
相關問題