-1
每次运行我的程序以获得两个随机生成的整数的总和时,结果总是为0 + 0.请让我知道我错过了什么。为什么我的随机发生器只产生0的?
public void question() {
System.out.println("What is " + Random1 + "+" + Random2 + "?");
userAnswer = scan.nextInt();
System.out.println("You entered: " + userAnswer);
}
public void actualAnswer() {
Answer = (Random1 + Random2);
System.out.println("The Correct Answer is: " + Answer);
if(userAnswer==(Answer)) {
System.out.println("You answered correctly!");
}
else {
System.out.println("You answered incorrectly");
}
}
private void random1() {
Random1 = random.nextInt(10) + 1;
}
private void random2() {
Random2 = random.nextInt(10) + 1;
}
尝试和避免使用大写的变量名称,这是相当混乱。另外,什么是“random”,“Random1”,“Random2”和“Answer”?您的代码不显示。 – fge
因为在将值分配给“答案”之前,您没有调用random1()和random2()方法。正如@fge建议的那样,阅读Java命名约定。 – Adi
@阿迪谢谢。我用问题的方法给他们打电话,现在正在工作 – Jb1128