我有一个代码可以检查wordpuzzle是否包含某个单词。 这是测试类:
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;
}
}
当我进行测试时,它会说它没有找到“BANANA”这个词。我找不到我的错误。它应该返回
wordFound=true
向下检查时。但它没有......
答案 0 :(得分:3)
您没有使用输入String初始化char数组。
更改
public boolean searchWord(String word) {
char[] wordChar;
wordChar = new char[word.length()];
...
到
public boolean searchWord(String word) {
char[] wordChar = word.toCharArray();
...
答案 1 :(得分:0)
在searchWord方法中(在WordPuzzle类中),您使用的是char数组:
wordChar = new char[word.length()];
但是你从未将String变量“word”的值设置到它中(你只是给它了它的长度)。
然后,这个if条件:
if (puzzleArray[yaxis][xaxis]!=wordChar[index]) {
xaxis++;
}
始终为true,并且您的方法只是递增变量并检查此无用条件,并以错误返回结束。
尝试更换
wordChar = new char[word.length()];
与
wordChar = word.toCharArray();