[英]While Loop Repeating forever when printing statement
I am making a Hangman Game where the user guesses a color. 我正在制作一个Hangman游戏,用户可以猜测一种颜色。 What I want to happen is: If the user gets more than 5 incorrectly, (went through all 5 hints), it will state that the user lost.
我想发生的事情是:如果用户错误地获得了5个以上的错误(通过所有5条提示),它将表明该用户已丢失。 After the while loop finishes (the word is correct or went through all 5 hints) it should tell you whether you won or not.
while循环结束后(单词正确或经过所有5条提示),它应该告诉您是否获胜。 This is the part of the code that does that:
这是执行此操作的代码部分:
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String color = getColor();
String result = EmptyStr(color);
String[] hints = getHints(color);
System.out.println("Please enter a letter:");
char letter = in.next().charAt(0);
//-----------------------------------------------------------
int wrong = 0;
while (wrong<6){
if (checkLetter(color, letter)){
// result was previously defined elsewhere so don't worry about it
result = Result(color, result, letter);
System.out.println("Correct guess! here is your word so far: " + result);
System.out.println("Lifelines left: " + (5 - wrong));
if (result.equals(color)){
System.out.println("congratulations! You Won!");
break;
}
}
else{
wrong ++;
System.out.println(hints[0]);
System.out.println("Lifelines left: " + (5 - wrong));
}
}
// also not sure what to put here after player wins or loses: System.out.println("Sorry! You lost! The correct word was: " + color);
} }
However, once I input one letter, and it is correct, result = Result(color, result, letter); System.out.println("Correct guess! here is your word so far: " + result); System.out.println("Lifelines left: " + (5 - wrong));
但是,一旦我输入了一个字母并且正确无误,
result = Result(color, result, letter); System.out.println("Correct guess! here is your word so far: " + result); System.out.println("Lifelines left: " + (5 - wrong));
result = Result(color, result, letter); System.out.println("Correct guess! here is your word so far: " + result); System.out.println("Lifelines left: " + (5 - wrong));
repeats forever. 永远重复。 I want to know: why does it repeat forever?
我想知道:为什么它会永远重复? and also, how can I fix it for it to work?
而且,我该如何对其进行修复?
I found my own problem! 我发现了自己的问题!
The thing is, in order for it to reiterate and allow me to re-enter a letter, char letter = in.next().charAt(0);
问题是,为了重申它并允许我重新输入一个字母,
char letter = in.next().charAt(0);
must be INSIDE the while
loop. 必须在
while
循环内。 That fixed the whole problem! 这解决了整个问题!
I think I see the issue. 我想我看到了问题。 You don't have an iteration for when the user gets the guess right, only when the get it wrong.
当用户正确猜对时,没有迭代,只有在错误猜对时才有迭代。 Make sure to add another "wrong ++", in that first if.
确保首先添加另一个“错误的++”。 That might solve the problem.
那可能会解决问题。
if (checkLetter(color, letter)){
// result was previously defined elsewhere so don't worry about it
result = Result(color, result, letter);
System.out.println("Correct guess! here is your word so far: " + result);
System.out.println("Lifelines left: " + (5 - wrong));
wrong ++;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.