This is a simple program that generates two random numbers between 0 and 1000, then has the user enter the sum of the two numbers. The if statement always evaluates as incorrect though even if you input the correct answer and sum and answer match.
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);
}
}