【问题标题】:How to randomly position a variable within X + Y = Z operation and loop如何在 X + Y = Z 操作和循环中随机定位变量
【发布时间】:2014-09-13 21:42:29
【问题描述】:

我基本上希望能够循环 X + Y = Z 等式,直到用户输入除整数以外的其他内容,例如字母“A”,并且当有任何数字时,循环停止显示消息。

另外,我对如何随机定位“?”感到困惑。用户必须输入正确答案。

例如

System.out.println("What is: " + num1 + " + ? = " + answer);

到目前为止:

我正在定位“?”手动通过 IF 语句。能否以更有效的方式完成?

public static void main(String[] args) {


    Random rand = new Random();

    Scanner input = new Scanner(System.in);

    int num1, num2, number3, answer;


    do {

        num1= 1 + rand.nextInt(10);
        num2= 1 + rand.nextInt(10);

        answer= num1 + num2;
        System.out.println("What is: " + num1 + " + ? = " + answer);

        number3= input.nextInt();

        if (number3 == num2)
            System.out.println("That is correct");
        else 
            System.out.println("That is wrong");

        num1= 1 + rand.nextInt(10);
        num2= 1 + rand.nextInt(10);

        answer= num1 + num2;

        System.out.println(num1 + " + ? = " + answer);

        number3= input.nextInt();

    } while(number3 !=0);       
}

【问题讨论】:

  • 您是否尝试过 try/catch 块来检测非整数输入?例如尝试{ number3 = input.nextInt(); }catch(异常 e){ 中断;我还没有测试过,但它看起来是检测无效输入的好方法。
  • 看起来你在做同样的事情两次。代码看起来像是在您的 if..else 语句之后重复。也许考虑删除它。您能否详细说明“我正在通过 IF 语句手动定位“?”?
  • I am positioning the "?" manually through the IF statements. 不,您不是,您将? 定位在 print 语句定位它的位置。基本上你想要的是检查输入是否是任何整数?
  • @Tuan333 我会尝试使用catch/try,谢谢!
  • @clever_trevor 很抱歉造成混乱,我希望能够更改用户看到“?”所在的位置。有什么想法吗?

标签: java loops variables random


【解决方案1】:

这是一种方法:

public static void main(String[] args) {

    Random rand = new Random();
    Scanner scanner = new Scanner(System.in);
    int[] xyz = new int[3];
    String[] display = new String[3];
    int answer, position;

    do {
        xyz[0] = 1 + rand.nextInt(10);
        xyz[1] = 1 + rand.nextInt(10);
        xyz[2] = xyz[0] + xyz[1];
        for (int i = 0; i < xyz.length; i++)
            display[i] = String.valueOf(xyz[i]);
        position = rand.nextInt(3);
        display[position] = "?";
        System.out.println("What is: " + display[0] + " + " + display[1] + " = " + display[2]);

        do {
            System.out.println("Please enter an integer or 'S' to stop");
            String input = scanner.nextLine();
            if (input.equals("S")) {
                scanner.close();
                System.out.println("Stopped");
                return;
            }
            else if (input.matches("\\d+")) { // \\d+ means "a digit (0-9) once or more
                answer = Integer.parseInt(input);
                break;
            }
        } while (true);

        if (answer == xyz[position])
            System.out.println("That is correct");
        else
            System.out.println("That is wrong");
    } while (true);
}

注意事项:

  • 我使用内部 do-while 循环反复检查输入并要求用户提供有效输入。
  • 我使用 2 个数组:一个用于存储数字,另一个用于存储显示值。
  • 我还添加了一个停止条件,因为外部循环是无限的。完成后请务必关闭Scanner
  • 我在 3 个位置中随机选择 1 个作为“?”。

【讨论】:

  • 感谢您的意见!但是,由于我还没有真正学会 try and catch,我正在寻找其他方法来解决这个问题。一个想法是,我不知道这是否可能。我正在考虑创建一个 do-while 循环,并且在该循环内有一个 if 语句不断检查不是整数的答案。还有一个打印特定消息的键盘输入。我是编程新手,不知道是否有这样的语法可用。例如:如果 user.input != integer; println("请输入一个整数");
  • @Tor 我看不出这与“如何在 X + Y = Z 操作和循环中随机定位变量?”这个问题有什么关系。你必须非常清楚你到底想要什么。您还必须告诉我们您可以使用什么。
猜你喜欢
  • 2018-06-08
  • 2013-04-17
  • 2019-09-27
  • 2019-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-26
  • 1970-01-01
相关资源
最近更新 更多