[英]Why does my program not stop?
Can anyone tell me, when I'm hitting y/n - why does my game not stop, it only starts again. 任何人都可以告诉我,当我打y / n时 - 为什么我的比赛没有停止,它只会再次开始。 I'm calling my
isDone()
method before the run, but it just runs over and over again and again. 我在运行之前调用了我的
isDone()
方法,但它只是一遍又一遍地运行。
public class Model {
int numberToGuess = 29;
int counter;
boolean isDone;
Scanner sc = new Scanner(System.in);
public Model() {
isDone = false;
run();
}
public void run() {
welcomeMessage();
while (true) {
guessNumber();
}
}
public void welcomeMessage() {
System.out.println("Welcome to " + '\n' + "***** GUESS THE NUMBER *****");
}
public void guessNumber() {
System.out.println("Please enter number and hit 'Enter'" + '\n');
if (sc.hasNextInt()) {
int input = sc.nextInt();
counter++;
if (input < numberToGuess) {
System.out.println('\n' + "Your guess is too low!" + '\n');
} else if (input > numberToGuess) {
System.out.println('\n' + "Your guess is too high!" + '\n');
} else {
System.out.println("Congratulations, you guessed the number!" + '\n' + "You guessed the number in " + counter + " guess." + '\n');
isDone();
}
} else {
System.out.println("Invalid input, please enter a number!" + '\n');
sc.next();
}
}
public void isDone() {
System.out.println("Do you wanna play again? Enter y/n");
if (sc.hasNext()) {
String input = sc.next();
if ("y".equals(sc))
if (input.equals("y")) {
isDone = false;
guessNumber();
} else if (input.equals("n")) {
isDone = true;
}
}
System.out.println("Invalid input, please enter y/n to continue");
sc.next();
}
}
You have an infinite loop: 你有一个无限循环:
while (true)
Theres no condition to terminate the program. 没有条件终止该计划。
You could use your global isDone
variable and replace the true
with: 您可以使用全局
isDone
变量并将true
替换为:
while (!isDone)
{
guessNumber();
}
isDone() method has logical flaw. isDone()方法有逻辑缺陷。 I have corrected it.
我纠正了它。
Step 1: Change while (true)
to while (!isDone)
第1步:将
while (true)
更改为while (!isDone)
Step 2: Corrected "y" condition for input and commented last two lines after making isDone=true, which is causing program to continue even after entering "n" 步骤2:修正输入的“y”条件,并在生成isDone = true后注释最后两行,这导致程序在输入“n”后继续
Modified code: 修改后的代码
public void isDone() {
System.out.println("Do you wanna play again? Enter y/n");
if (sc.hasNext()) {
String input = sc.next();
if ("y".equals(input)) {
isDone = false;
guessNumber();
} else if (input.equals("n")) {
isDone = true;
}
}
//System.out.println("Invalid input, please enter y/n to continue");
//sc.next();
}
EDIT: Explanation of flaw in while condition. 编辑:解释条件中的缺陷。
You are comparing scanner object with "y" instead of input value read from the scanner with "y" 您正在将扫描仪对象与“y”进行比较,而不是使用“y”从扫描仪读取的输入值
one small change you need to do 你需要做一个小小的改变
public void run() {
welcomeMessage();
//while (true) {
while (!isDone) {
guessNumber();
}
}
Okay, now it's working :). 好的,现在它正在工作:)。 The solution:
解决方案:
public class Model { 公共类模型{
int numberToGuess = 29;
int counter;
boolean isDone;
Scanner sc = new Scanner(System.in);
public Model()
{
isDone = false;
run();
}
public void run()
{
welcomeMessage();
while (!isDone)
{
guessNumber();
}
}
public void welcomeMessage()
{
System.out.println("Welcome to " + '\n' + "***** GUESS THE NUMBER *****");
}
public void guessNumber()
{
System.out.println("Please enter number and hit 'Enter'" + '\n');
if (sc.hasNextInt())
{
int input = sc.nextInt();
counter++;
if (input < numberToGuess)
{
System.out.println('\n' + "Your guess is too low!" + '\n');
}
else if (input > numberToGuess)
{
System.out.println('\n' + "Your guess is too high!" + '\n');
}
else
{
System.out.println("Congratulations, you guessed the number!" + '\n' + "You guessed the number in " + counter + " guess." + '\n');
isDone();
}
}
else
{
System.out.println("Invalid input, please enter a number!" + '\n');
sc.next();
}
}
public void isDone()
{
System.out.println("Do you wanna play again? Enter y/n");
if (sc.hasNext())
{
String input = sc.next();
if (input.equals("y"))
{
isDone = false;
counter = 0;
}
else if (input.equals("n"))
{
isDone = true;
}
else
{
System.out.println("Invalid input");
isDone();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.