Re: Can't fix my own errors
Did you bother to read the error message? It says that it can not find the variable called answer. I can't either. Can you?
Re: Can't fix my own errors
Ok so, I got the right code finally
mport javax.swing.JOptionPane;
public class RandomNumbers {
public static void main (String[] args) {
//Generate three random numbers
int one, two, three;
one = (int)(Math.random()*(10));
two = (int)(Math.random()*(10));
three = (int)(Math.random()*(10));
//What is the sum of these numbers
String sumOfNumbersString =
JOptionPane.showInputDialog(null,"What is the sum of " + one + "+" +
two + "+" + three + "?");
//Convert string to int
int sumOfNumbers =Integer.parseInt(sumOfNumbersString);
//Check the answer and Display Results
if (one + two + three == answer)
JOptionPane.showMessageDialog(null, "You are Correct!");
else
JOptionPane.showMessageDialog(null, "Sorry, that is Incorrect \n" +
one + "+" + two + "+" + three + " equals " + (one + two + three));
}
}
I simply changed == answer to sumOfNumbers and all worked wonderfully!!
Re: Can't fix my own errors
Quote:
Originally Posted by
mrroberts2u
Ok so, I got the right code finally
...
I simply changed == answer to sumOfNumbers and all worked wonderfully!!
So why don't you post the corrected code? Why post the bad code again?
Re: Can't fix my own errors
import javax.swing.JOptionPane;
public class RandomNumbers {
public static void main (String[] args) {
//Generate three random numbers
int one, two, three;
one = (int)(Math.random()*(10));
two = (int)(Math.random()*(10));
three = (int)(Math.random()*(10));
//What is the sum of these numbers
String sumOfNumbersString =
JOptionPane.showInputDialog(null,"What is the sum of " + one + "+" +
two + "+" + three + "?");
//Convert string to int
int sumOfNumbers =Integer.parseInt(sumOfNumbersString);
//Check the answer and Display Results
if (one + two + three == sumOfNumbers)
JOptionPane.showMessageDialog(null, "You are Correct!");
else
JOptionPane.showMessageDialog(null, "Sorry, that is Incorrect \n" +
one + "+" + two + "+" + three + " equals " + (one + two + three));
}
}