gpt4 book ai didi

java While-loop语句将继续循环但不会包含我退出循环的语句

转载 作者:行者123 更新时间:2023-12-02 04:19:06 25 4
gpt4 key购买 nike

程序本身可以工作,但我在循环中遇到了困难。我是使用循环的初学者,我一直在寻找适用于我的程序类型的示例,但遗憾的是没有找到任何示例。

我知道如果体内有某些东西可以使条件为“假”,循环就会停止,但对于这个程序来说,当它询问用户是否想再次播放时就会决定。但是,我的程序不会询问用户是否要继续。如果用户决定停止,那么程序应该说“感谢您玩!”而不是继续循环整个主体。

感谢您的帮助!

这是我的循环:

// Start loop
while (looping == true) {

// Ask if the # > 5
System.out.println("Is your number greater than 5? (True = 1, False = 0)");
// Read in the number
numberOne = input.nextDouble();

if (numberOne == 1) { // Is the # > 7?
System.out.println("Is your number greater than 7? (True = 1, False = 0)");
numberOne = input.nextDouble();

if (numberOne == 1) { // Does the # = 8?
System.out.println("Is your number 8? (True = 1, False = 0)");
numberOne = input.nextDouble();

if (numberOne == 0) { // If # != 8, then # = 9
System.out.println("Your number is 9!");
} else {
// If answer was 8, then yay
System.out.println("Yay! Got it!");
}
} else if (numberOne == 0) { // If # !> 7, then # = 6?
System.out.println("Is your number 6? (True = 1, False = 0)");
numberOne = input.nextDouble();

if (numberOne == 0) { // If # != 6, then # = 7
System.out.println("Your number is 7!");
} else {
//If answer was 6, then yay
System.out.println("Yay! Got it!");
}
}

} else if (numberOne == 0) { // If the # !> 5, then # > 3?
System.out.println("Is your number greater than 3? (True = 1, False = 0)");
numberOne = input.nextDouble();

if (numberOne == 1) { // If true, does your number = 4?
System.out.println("Is your number 4? (True = 1, False = 0)");
numberOne = input.nextDouble();

if (numberOne == 0) { // If # != 4, then # = 5
System.out.println("Your number is 5!");
} else {
// If answer was 4, then yay
System.out.println("Yay! Got it!");
}
} else if (numberOne == 0) { // If false, # = 2?
System.out.println("Is your number 2? (True = 1, False = 0)");
numberOne = input.nextDouble();

if (numberOne == 0) { // If # != 2, then # = 3
System.out.println("Your number is 3!");
} else {
// If answer is 2, then yay
System.out.println("Yay! Got it!");
}

// Ask user if they want to play again
System.out.println("Would you like to play again? (Yes = 1, No = 0)");
numberOne = input.nextDouble();

System.out.println("Thanks for playing!");
}
}
} // end loop
// Close input
input.close();
} // end method
} // end class

最佳答案

因为您没有更改循环变量值。并将下面的代码移到 else if() block 之外。

...
System.out.println("Would you like to play again? (Yes = 1, No = 0)");
looping = input.nextBoolean();

if (looping == false) {
System.out.println("Thanks for playing!");
}
...
<小时/>

您的代码将如下所示

// Start loop
while (looping == true) {

// Ask if the # > 5
System.out.println("Is your number greater than 5? (True = 1, False = 0)");
// Read in the number
numberOne = input.nextDouble();

if (numberOne == 1) { // Is the # > 7?
System.out.println("Is your number greater than 7? (True = 1, False = 0)");
numberOne = input.nextDouble();

if (numberOne == 1) { // Does the # = 8?
System.out.println("Is your number 8? (True = 1, False = 0)");
numberOne = input.nextDouble();

if (numberOne == 0) { // If # != 8, then # = 9
System.out.println("Your number is 9!");
} else {
// If answer was 8, then yay
System.out.println("Yay! Got it!");
}
} else if (numberOne == 0) { // If # !> 7, then # = 6?
System.out.println("Is your number 6? (True = 1, False = 0)");
numberOne = input.nextDouble();

if (numberOne == 0) { // If # != 6, then # = 7
System.out.println("Your number is 7!");
} else {
//If answer was 6, then yay
System.out.println("Yay! Got it!");
}
}

} else if (numberOne == 0) { // If the # !> 5, then # > 3?
System.out.println("Is your number greater than 3? (True = 1, False = 0)");
numberOne = input.nextDouble();

if (numberOne == 1) { // If true, does your number = 4?
System.out.println("Is your number 4? (True = 1, False = 0)");
numberOne = input.nextDouble();

if (numberOne == 0) { // If # != 4, then # = 5
System.out.println("Your number is 5!");
} else {
// If answer was 4, then yay
System.out.println("Yay! Got it!");
}
} else if (numberOne == 0) { // If false, # = 2?
System.out.println("Is your number 2? (True = 1, False = 0)");
numberOne = input.nextDouble();

if (numberOne == 0) { // If # != 2, then # = 3
System.out.println("Your number is 3!");
} else {
// If answer is 2, then yay
System.out.println("Yay! Got it!");
}
}
}

// Ask user if they want to play again
System.out.println("Would you like to play again? (Yes = 1, No = 0)");
looping = input.nextBoolean();

if (looping == false) {
System.out.println("Thanks for playing!");
}
}
// Close input
input.close();
}
}

关于java While-loop语句将继续循环但不会包含我退出循环的语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32984325/

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