gpt4 book ai didi

java - Hangman while 循环不适用于 ||

转载 作者:行者123 更新时间:2023-12-01 19:49:29 25 4
gpt4 key购买 nike

我正在制作的 Hangman 游戏有以下 while 循环:

while(amountOfGuessesLeft > 0 || !wordComplete){
System.out.println("The word now looks like this: " + filteredWord);
System.out.println("You have " + amountOfGuessesLeft + " guesses left");
System.out.print("Your guess: ");
try {
guess = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(guess.length() == 0 || guess.length() > 1){
System.out.println("Please just type one letter. Try again.");
}else if(wordSelected.contains(guess) || wordSelected.contains(guess.toUpperCase())){
System.out.println("That guess is correct.");
filteredWord = showGuess(filteredWord, wordLetters, guess.toUpperCase().charAt(0));
if(!filteredWord.contains("_")){
wordComplete = true;
}
}else{
System.out.println("There are no " + guess.toUpperCase() + "'s in this word.");
amountOfGuessesLeft--;
}
}

当 while 循环条件正好 amountOfGuessesLeft > 0 时,一切正常。但是,当我添加||时!wordComplete,这两个条件都不适用,循环永远不会结束。它也不会抛出任何错误。任何帮助将不胜感激。

最佳答案

您需要使用&&

只要还有任何猜测并且用户尚未找到该单词,您就希望运行循环。

目前,如果您用尽了猜测但仍然没有找到该单词,!wordComplete 将计算为 true 并允许再次执行循环。

关于java - Hangman while 循环不适用于 ||,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51978402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com