View RSS Feed

TaIraonline

Using Random class to generate two numbers

Rate this Entry
by , 02-27-2012 at 10:42 PM (949 Views)
Guys can you please help me out here, I'm using JOptionPane to receive two values from the user I then use these values as a range for my random generator. I then ask the user what's the answer based on a question randomly generated, but since I'm asking a division question how can I make sure that the first value is always bigger than the second?

Random rand = new Random();
it val1 = 0;
it val2 = 0;
it start = Integer.parseInt(JOptionPane.showInputDialog("enter first range "));

it last = Integer.parseInt(JOptionPane.showInputDialog("enter last range "));

val1 = rand.nextInt(last) + first;
val2 = rand.nextInt(last) + first;

String mess = String.format("what is %d/%d",val1,val2);
String div = JOptionPane.showInputDialog(mess);

double dvAns = Double.parseDouble(div);
double correct = math.quotient (val1,val2); //math is an object of the class that contains the quotient method

if(dvAns == correct)
JOptionPane.showMessageDialog(null,"good");
else
JOptionPane.showMessageDialog(null,"wrong");

Submit "Using Random class to generate two numbers" to Facebook Submit "Using Random class to generate two numbers" to Digg Submit "Using Random class to generate two numbers" to del.icio.us Submit "Using Random class to generate two numbers" to StumbleUpon Submit "Using Random class to generate two numbers" to Google

Tags: java Add / Edit Tags
Categories
Uncategorized

Comments

  1. fatabass's Avatar
    • |
    • permalink
    Post your questions in the Forum.