Java 如何生成5个随机问题?
我在编写生成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
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在循环中接受输入来获取该值。