【问题标题】:Iterating a While Loop with User Input使用用户输入迭代 While 循环
【发布时间】:2020-05-23 03:13:22
【问题描述】:

首先,感谢您阅读我的帖子并帮助我。

我正在尝试在我定义一个随机数的地方进行编程,程序猜测这个随机数,然后根据我说的猜测太高或太低,程序再次猜测。

当程序猜到一个太高的数字时,用户输入“lower”,然后程序只猜较低的数字。

理想情况下,程序会考虑所有先前选择的结果。因此,如果猜测 #1 太高,那么这应该是之后所有迭代的上限。我们不希望每次迭代都只改变上限或下限。

我尝试对我的代码执行的操作是重置用于计算随机数 (ThreadLocalRandom) 的最小值和最大值。

主要问题是我无法重置 oMin 和 oMax 值。我不明白如何解决这个问题,对不起,如果这是一个完全愚蠢的问题。

再次感谢!

import java.util.Scanner;

import java.util.concurrent.ThreadLocalRandom;

public class J4AB_S11_C3 {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int oMin = 0;
        int oMax = 101;
        int randomNumber = ThreadLocalRandom.current().nextInt(oMin, oMax);
        String userReply;

        System.out.println("What's the lucky number, kiddo?");
        int correctAnswer = scanner.nextInt();

        System.out.println("Is " + randomNumber + " the right answer?");
        userReply = scanner.next();

        while ("Higher".equalsIgnoreCase(userReply)) {
            oMin = randomNumber;
        }

        while ("Lower".equalsIgnoreCase(userReply)) {
            oMax = randomNumber;
        }

        if (userReply.equalsIgnoreCase("Correct")) {
            System.out.println("We finally did it! " + correctAnswer + " was the correct answer.");
        }

    }
}

【问题讨论】:

  • 您希望什么时候重置 oMin 和 oMax?您想将它们重置为什么(0 和 101)?
  • 我希望在猜测过高或过低时重置它们。如果正确答案是 20,但程序猜测为 40,我希望将 40 设置为新的最大值。
  • ^ 我希望随着时间的推移保留这些更改,以便猜测的范围不断变小。
  • 我想我明白了,请给我几分钟时间查看您的代码,我会尝试发布解决方案。
  • 当你应该使用 if 时,你正在使用 'while ("Higher".equalsIgnoreCase(userReply))'。

标签: java if-statement random while-loop user-input


【解决方案1】:

为此使用do-while,if-condition检查更高和更低。并在 do-while 之后关闭 scanner

        Scanner scanner = new Scanner(System.in);
        int oMin = 0;
        int oMax = 101;
        String userReply;
        int correctAnswer;
        do {
            int randomNumber = ThreadLocalRandom.current().nextInt(oMin, oMax);
            System.out.println("What's the lucky number, kiddo?");
            correctAnswer = scanner.nextInt();

            System.out.println("Is " + randomNumber + " the right answer?");
            userReply = scanner.next();

            if ("Higher".equalsIgnoreCase(userReply)) {
                oMin = randomNumber;
            }else if ("Lower".equalsIgnoreCase(userReply)) {
                oMax = randomNumber;
            }
        } while (!userReply.equalsIgnoreCase("Correct"));

        System.out.println("We finally did it! " + correctAnswer + " was the correct answer.");

        scanner.close();

【讨论】:

  • 只有在完成此程序的 System.in 后,才应关闭扫描程序。
  • 谢谢你!我要花几个小时才能找到它,所以在这里发布它是 1000% 值得的。不过我想问一下——不应该是最初的“幸运数字是多少?”并且正确答案的定义在do-loop之外。无论如何,谢谢一百万次!
【解决方案2】:

我做了一些编辑并添加了一个 while 循环,请检查一下。我测试了它,它似乎工作正常。

import java.util.Scanner;

import java.util.concurrent.ThreadLocalRandom;

public class J4AB_S11_C3 {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int oMin = 0;
        int oMax = 101;
        int randomNumber = ThreadLocalRandom.current().nextInt(oMin, oMax);
        String userReply;

        System.out.println("What's the lucky number, kiddo?");
        int correctAnswer = scanner.nextInt();

        System.out.println("Is " + randomNumber + " the right answer?");
        userReply = scanner.next();
        while (!"Correct".equalsIgnoreCase(userReply)){
        if ("Higher".equalsIgnoreCase(userReply)) {
            oMin = randomNumber;
        }

        if ("Lower".equalsIgnoreCase(userReply)) {
            oMax = randomNumber;
        }
        randomNumber = ThreadLocalRandom.current().nextInt(oMin, oMax);
        System.out.println("Is " + randomNumber + " the right answer?");
        userReply = scanner.next();
        }
        if (userReply.equalsIgnoreCase("Correct")) {
            System.out.println("We finally did it! " + correctAnswer + " was the correct answer.");
        }
    }
}

【讨论】:

  • 谢谢你!它也适用于我。如果我能给你一个赞成票,我会的,但我在这里太新了。
猜你喜欢
  • 2018-09-18
  • 2013-11-16
  • 2020-02-15
  • 1970-01-01
  • 1970-01-01
  • 2016-03-08
  • 1970-01-01
  • 2016-01-11
  • 1970-01-01
相关资源
最近更新 更多