如何生成5个随机问题?

时间:2014-10-27 16:36:02

标签: java

我无法编写正确的代码来生成5个随机数学问题,并能够输入答案并被告知是正确还是错误。这就是我到目前为止所拥有的

public static void addNumOneToTen(Scanner keyboard)
{
    int startValue = 1, endValue = 10;
    for (int i = 1; i <= 5; i++) 
    {
        System.out.println("Enter the answer to the problem:");
       int ranNumberOne = (int)(startValue + Math.random() * (endValue - startValue + 1));
       int ranNumberTwo = (int)(startValue + Math.random() * (endValue - startValue + 1));

       while (ranNumberOne < ranNumberTwo)
       {
       ranNumberOne = (int)(startValue + Math.random() * (endValue - startValue +1));
       ranNumberTwo = (int)(startValue + Math.random() * (endValue - startValue +1));
       }
    }
}

当我点击运行时,我会生成此

请为您的数学测验选择以下选项之一:
1.增加数字1-10
2.增加数字1-100
3.减号数字1-10
4.减去数字1-100
5.乘以数字1-10
6.退出程序

1
输入问题的答案:
输入问题的答案:
输入问题的答案:
输入问题的答案:
输入问题的答案:

建立成功(总时间:4秒)

我不知道我做错了什么。我以为我做得对。任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

您需要在循环中读取扫描仪中您未执行的操作。如下所示:

 double nextNumber = keyboard.nextDouble();//or nextInt for next integer.

http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextDouble%28%29