maths question random + or -
807599Dec 2 2006 — edited Dec 2 2006Hi,
I am a Jave newbie. I have to write a maths quiz program, the program generates 2 random number and must randomly select the operator + or -. Question and answer are output to the screen and the user inputs the answer. the only part I have aproblem with is generating and displaying the + or - operator.
the code below looks at the 2nd random number and if it is <4 then operator is set to minus. This does not work can anyone help.
The code.
a= myRandom.nextInt(8) +11;//generates random number
b = myRandom.nextInt(8) +1;//generates 2nd random number
if(b<4)//this code sets random operator + or -
{
t='2'; // t is the operator
}
else
{
t='5';
}
System.out.print("Question "+p+": "+((a+t+b)+"= ? "));//prints question to screen
Thanks