Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

maths question random + or -

807599Dec 2 2006 — edited Dec 2 2006
Hi,
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 30 2006
Added on Dec 2 2006
13 comments
130 views