Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何生成5个随机问题?_Java - Fatal编程技术网

Java 如何生成5个随机问题?

Java 如何生成5个随机问题?,java,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 p

我在编写生成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));
       }
    }
}
publicstaticvoid addNumOneToTen(扫描仪键盘)
{
int startValue=1,endValue=10;

对于(int i=1;i,您需要从循环中的扫描器读取未执行的值。例如:

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

您当前的代码只运行循环5次,从未收到输入。是的,因此OP需要从系统输入中获取值,OP可以通过调用keyboard.nextDouble在循环中接受输入来获取该值。