我无法编写正确的代码来生成 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/