java - 如何生成 5 个随机问题?

标签 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秒)

我不知道我做错了什么。我原以为我是对的。有人能帮忙吗?

最佳答案

您需要在循环内从扫描仪读取您没有执行的值。所以类似:

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

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

关于java - 如何生成 5 个随机问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26592354/

相关文章:

java - request.getRequestDispatcher().forward(request, response) 在新页面

java - 在 Java 中初始化子类参数的首选方法?

java - 在 javascript 中使用 Apache Poi 解析 Excel 工作表

java - Ray 和 intersectRayTriangle 如何工作?

java.lang.IllegalStateException : Could not execute method of the activity

java - 在 eclipse 中设置测试输入

java - 如何在应用程序或 Activity 加载之前显示进度对话框?

java - 关闭 DAO 对象中的连接

java - Spring 启动测试 : @Sql annotation Unable to locate sql files placed in src/test/resources

java.lang.IllegalArgumentException : Comparison method violates its general contract