【问题标题】:unable to re-loop through a while loop successfully无法成功通过 while 循环重新循环
【发布时间】:2017-07-02 06:39:04
【问题描述】:

我在以下代码中的目标是不断猜测,直到用户猜对或退出。要退出,我可以轻松地跳出我的循环,但是当我尝试继续我的循环时,它不能正常工作。首先它需要多个输入,然后也完全重新生成我的号码,而我想要做的是不断猜测(询问用户)相同的随机数。 以下是我的代码:

public class Test {
public static void main(String[] args) {
    int count, randNum, guess;
    count = 0;
    Scanner scan = new Scanner(System.in);

    while (true) {
        Random rand = new Random();
        randNum = rand.nextInt(100) + 1;
        System.out.println("Guess a number b/w 1 and 100");
        guess = scan.nextInt();
        count += 1;

        if (guess == randNum) {
            System.out.println("Correct guess.");
            System.out.println("It took " + count + " tries to guess the right number");
            System.out.println("Would you like to play again? ");
            System.out.println("Press any letter to play again or q to quit: ");
            if (scan.next().charAt(0) == 'q' || scan.next().charAt(0) == 'Q') {
                break;
            }
            else{
                continue;
            }
        }
        if (guess > randNum) {
            System.out.println("Your guess is bigger than actual number. Would you like to try again?");
            System.out.println("Press q to quit or any other letter to try again");
            if (scan.next().charAt(0) == 'q' || scan.next().charAt(0) == 'Q') {
                break;
            }
            else {
                continue;
            }
        }
        else if (guess < randNum) {
            System.out.println("Your guess is smaller than actual number. Would you like to try again?");
            System.out.println("Press q to quit or any other letter to try again");
            if (scan.next().charAt(0) == 'q' || scan.next().charAt(0) == 'Q') {
                break;
            }
            else  {
                    continue;
                }
            }

        }

}

}

【问题讨论】:

  • 谈论一个移动的目标 - randNum 正在循环的每次迭代中重新设置
  • 您需要将游戏和尝试分成外/内循环。

标签: java if-statement while-loop conditional


【解决方案1】:

生成随机数的代码应该在while语句之前。当您调用continue 时,它会返回到while 块的第一行,从而生成另一个随机数。

【讨论】:

  • 感谢您的帮助。它确实解决了这个问题,但现在还有一个问题。当我输入一个字母以继续时,我必须在循环重新启动之前输入大约 3 次。我不明白为什么会这样
【解决方案2】:

您声明 int randNum 的语句位于 while 循环内,因此每次 while 循环重复时,都会(再次)声明该数字并将其设置为 1 到 100 之间的值。

如果您想防止这种情况发生,请在 while 循环之外声明变量并使用随机值对其进行初始化。

一个小提示:通过在 while 循环内初始化变量,您可能比您想要的更多地限制了它的范围。每次循环时,您创建的先前 randNum 不再存在,然后它会创建一个新的。基本上,如果您希望它更永久,请在循环之外对其进行初始化。

此外,如果您只想让它在第一次请求 1 到 100 之间的数字,请将其移出循环。但是,这取决于您是否希望它每次都询问,还是只询问一次。

//…
public static void main(String[] args) {
    int count, randNum, guess;
    count = 0;
    Scanner scan = new Scanner(System.in);
    Random rand = new Random();
    randNum = rand.nextInt(100) + 1;
    System.out.println("Guess a number b/w 1 and 100");            
    while (true) {
        /*Random rand = new Random();
        randNum = rand.nextInt(100) + 1;
        System.out.println("Guess a number b/w 1 and 100");*/

        guess = scan.nextInt();
        count += 1;
        //…

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    • 2018-03-05
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    相关资源
    最近更新 更多