【发布时间】:2015-05-11 00:05:59
【问题描述】:
这是一个简单的程序,它生成 0 到 1000 之间的两个随机数,然后让用户输入这两个数字的总和。即使您输入了正确的答案并且总和与答案匹配,if 语句也始终评估为不正确。
import java.util.Random;
import java.util.Scanner;
public class mathquiz
{
public static void main(String[] args)
{
Integer num1;
Integer num2;
Integer sum;
Integer answer;
Scanner input = new Scanner(System.in);
Random rand = new Random();
num1 = rand.nextInt(1000); //random number between 0 and 1000
rand = new Random();
num2 = rand.nextInt(1000); //random number between 0 and 1000
System.out.println(" "+num1);
System.out.println("+ "+num2);
sum = num1 + num2; //adding the two random numbers together
answer = input.nextInt();
System.out.println(sum); //test print to see what sum is
System.out.println(answer); //test print to see what answer is
if (sum == answer) //always evaluates as incorrect, I would like to know why
System.out.println("Congratulations! You are correct!");
else
System.out.println("You were incorrect. The correct answer is: "+sum);
}
}
【问题讨论】:
标签: java