1 回答
TA贡献1851条经验 获得超3个赞
是的。从checking上返回true。正如您现在所拥有的那样,只有最后一个单词匹配时,它才能为真。喜欢,
public static boolean checking(String[] dictionary, String userWord) {
for ( int i =0; i < dictionary.length; i++) {
if (userWord.equals(dictionary[i])) {
return true;
}
}
return false;
}
此外,您需要dictionary通过向数组中添加单词来填充您的内容。而且,我更喜欢try-with-resources显式close()调用。就像是,
public static String[] dictionary(String filename) throws FileNotFoundException {
final String fileName = "dictionary.txt";
int dictionaryLength = 0, i = 0;
try (Scanner dictionary = new Scanner(new File(fileName))) {
while (dictionary.hasNextLine()) {
++dictionaryLength;
dictionary.nextLine();
}
}
String[] theWords = new String[dictionaryLength];
try (Scanner dictionary = new Scanner(new File(fileName))) {
while (dictionary.hasNextLine()) {
theWords[i] = dictionary.nextLine();
i++;
}
}
return theWords;
}
添加回答
举报