我很难写出生成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));
}
}
}当我点击run时,我会生成以下代码
请为您的数学测验选择以下选项之一:
数字为1-10的数字加法1-100
1
输入问题的答案:
输入问题的答案:
输入问题的答案:
输入问题的答案:
输入问题的答案:
构建成功(总时间:4秒)
我不知道我做错了什么。我以为我是对的。有人能帮上忙吗?
发布于 2014-10-28 00:43:12
你需要在循环中从scanner中读取值,而你并没有这样做。所以就像这样:
double nextNumber = keyboard.nextDouble();//or nextInt for next integer.http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextDouble%28%29
https://stackoverflow.com/questions/26592354
复制相似问题