为什么这段代码 return false 而不是 true?
Why does this code return false instead of 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;
}
}
当我运行测试时,它会说没有找到"BANANA"这个词。我找不到我的错误。应该 return
wordFound=true
向下检查时。但它没有...
您没有使用输入字符串初始化 char 数组。
改变
public boolean searchWord(String word) {
char[] wordChar;
wordChar = new char[word.length()];
...
至
public boolean searchWord(String word) {
char[] wordChar = word.toCharArray();
...
在 searchWord 方法中(在 WordPuzzle class 中),您使用的是一个字符数组:
wordChar = new char[word.length()];
但是您从未将字符串变量 "word" 的值设置到其中(您只是给了它它的长度)。
那么,这个 if 条件:
if (puzzleArray[yaxis][xaxis]!=wordChar[index]) {
xaxis++;
}
始终为真,您的方法只是递增变量并检查此无用条件,并以假 return.
结束
尝试更换
wordChar = new char[word.length()];
与
wordChar = word.toCharArray();
我有一个代码可以检查字谜是否包含某个单词。 这是测试类:
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"这个词。我找不到我的错误。应该 return
wordFound=true
向下检查时。但它没有...
您没有使用输入字符串初始化 char 数组。
改变
public boolean searchWord(String word) {
char[] wordChar;
wordChar = new char[word.length()];
...
至
public boolean searchWord(String word) {
char[] wordChar = word.toCharArray();
...
在 searchWord 方法中(在 WordPuzzle class 中),您使用的是一个字符数组:
wordChar = new char[word.length()];
但是您从未将字符串变量 "word" 的值设置到其中(您只是给了它它的长度)。
那么,这个 if 条件:
if (puzzleArray[yaxis][xaxis]!=wordChar[index]) {
xaxis++;
}
始终为真,您的方法只是递增变量并检查此无用条件,并以假 return.
结束尝试更换
wordChar = new char[word.length()];
与
wordChar = word.toCharArray();