jeudi 28 janvier 2016

How to check if an input answer to a randomly generated mathematical equation is correct

I have a Java program that generates 2 random numbers and asks a user for a mathematical operator and uses those 3 elements to ask the user an equation. I am trying to check if the answer the user inputs is correct and display either correct or incorrect.

RandomGenerator rand = new RandomGenerator();
x=rand.nextInt(-10,10);
y=rand.nextInt(-10,10);

op=readLine("Choose an operator (+, -, /, or *): "); 
equ = x + op + y + "= "; 
val = readInt(equ); 
z = x + op + y 
if(z == val) { 
    println("CORRECT!!"); 
} 
if(z != val) { 
    println("Incorrect."); 
}




Aucun commentaire:

Enregistrer un commentaire