简体   繁体   English

为什么这段代码会打印出无数的“您的猜测是什么?”

[英]Why this code prints an infinite amount of “What's your guess?”

I'm trying to make a number guessing game between the range from 1 to 100. The program will stop if the user guesses the correct number. 我正在尝试制作一个介于1到100之间的数字猜谜游戏。如果用户猜测正确的数字,该程序将停止。

My current code: 我当前的代码:

import java.util.Scanner;
import java.util.Random;

public class GuessingGame {
    public static void main(String[] args) {
        Random randomNumber = new Random();
        Scanner reader = new Scanner(System.in);
        System.out.println("What's your guess? ");
        int n = reader.nextInt();
        reader.close();

    int Number = randomNumber.nextInt(100 + 1);
    int guess = n;
     while (guess != Number) {
        System.out.println("What's your guess? ");
        new Scanner(System.in);
    }

    if (guess == Number) {
        System.out.println("Your have guessed the correct number! ");
    }

    else if (guess < Number) {
        System.out.println("Your number is too low! ");
    }

    else if (guess > Number) {
        System.out.println("Your number is too high! ");
    }
}   
}            

This is where using the formatter in your IDE is helpful. 这就是在IDE中使用格式化程序的地方。 You would be able to see that the if conditions which are after the loop should be inside the loop and you should be reading the reading the guess from input. 您将能够看到循环之后的if条件应该在循环内部,并且您应该正在读取来自输入的猜测。

BTW You should never close the input unless you explicitly don't want to read anything from it ever again. 顺便说一句,除非明确不想再次从中读取任何内容,否则切勿关闭输入。

try 尝试

 Random randomNumber = new Random();
 Scanner reader = new Scanner(System.in);
 int number = randomNumber.nextInt(100 + 1);
 while(true) {
    System.out.println("What's your guess? ");
    int guess = reader.nextInt();

    if (guess == Number) {
        System.out.println("You have guessed the correct number! ");
        break; // no need to guess more

    } else if (guess < Number) {
        System.out.println("Your number is too low! ");

    } else if (guess > Number) {
        System.out.println("Your number is too high! ");
    }
}   

Change your while loop to 将您的while循环更改为

while (guess != Number) {
    if (guess == Number) {
     System.out.println("Your have guessed the correct number! ");
     break;
    }

    else if (guess < Number) {
     System.out.println("Your number is too low! ");
    }

    else if (guess > Number) {
     System.out.println("Your number is too high! ");
    }

    System.out.println("Guess Again?");
    guess = reader.nextInt();
}

The while loop is infinite loop. while循环是无限循环。 Also, you should avoid using 'Number' as variable name as there is a class in java with the same name java.lang.Number . 另外,您应该避免使用'Number'作为变量名,因为java中存在一个具有相同名称java.lang.Number You can also need any number of variables using single object of Scanner class. 您还可以使用Scanner类的单个对象来使用任意数量的变量。 So, a cleaner code would look like this : 因此,更简洁的代码如下所示:

while (guess != number) {
        System.out.println("What's your guess? ");
        guess = reader.nextInt();
    }

Now, if you solve above problems, your code will never go to the two else-if blocks, because by the time your while loop breaks you will have 'guess' always be equal to 'number'. 现在,如果您解决了上述问题,您的代码将永远不会进入两个else-if块,因为到while循环中断时,您的'猜测'总是等于'number'。 I think what you are trying to achieve here is : 我认为您要在这里实现的目标是:

while (guess != number) {
    System.out.println("What's your guess? ");
    guess = reader.nextInt();

    if (guess == number) {
        System.out.println("Your have guessed the correct number! ");
    }

    else if (guess < number) {
        System.out.println("Your number is too low! ");
    }

    else if (guess > number) {
        System.out.println("Your number is too high! ");
    }
}    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM