我正在为一个项目创建一个单词搜索游戏程序,并想知道我想要做的事情是否可能。下面是遵循我的项目指导方针的isPuzzleWord方法,即如果单词正确,它必须从数组中返回单词类对象,否则返回null。我的isPuzzleWord方法工作得很好。
public static Word isPuzzleWord (String guess, Word[] words) {
for(int i = 0; i < words.length; i++) {
if(words[i].getWord().equals(guess)) {
return words[i];
}
}
return null;
}我的问题是,我如何将这两个响应合并到一个if语句中,以便如果猜测是正确的,我可以继续游戏,如果猜测是错误的,我可以向用户提供反馈。
public static void playGame(Scanner console, String title, Word[] words, char[][] puzzle) {
System.out.println("");
System.out.println("See how many of the 10 hidden words you can find");
for (int i = 1; i <= 10; i++) {
displayPuzzle(title, puzzle);
System.out.print("Word " + i + ": ");
String guess = console.next().toUpperCase();
isPuzzleWord(guess,words);
if (
}
}发布于 2016-04-09 09:54:57
您只需将所调用的函数放入if子句中:
if (isPuzzleWord(guess,words) == null)或您想要测试的任何东西。
发布于 2016-04-09 10:03:54
尝试以下if-else函数:
if (isPuzzleWord(guess, words) == null){
System.out.println("Your Feedback"); //this could be your feedback or anything you want it to do
}如果从isPuzzleWord返回的结果为空,则您可以提供反馈,否则将意味着单词匹配,您可以继续播放,而无需进一步的操作。
发布于 2016-04-09 10:05:49
您可以存储返回单词的引用,以便在if语句之后使用。
Word word = isPuzzleWord(guess,words);
if (word == null) {
System.out.println("its not a Puzzle Word");
} else {
//you could access `word` here
}https://stackoverflow.com/questions/36511860
复制相似问题