I'm in the process of making a java quiz, when I noticed that I get an error if someone enters anything besides a number. Solution- I need a try catch statement.
Could someone show me the most effecient way to put that it my code?
Could someone show me the most effecient way to put that it my code?
Code:
// Question #1
++counter1;
++counter2;
value1 = 1 + ( int ) ( Math.random() * 50 );
value2 = 1 + ( int ) ( Math.random() * 50 );
value3 = 1 + ( int ) ( Math.random() * 50 );
value4 = 1 + ( int ) ( Math.random() * 50 );
value5 = 1 + ( int ) ( Math.random() * 50 );
zVal = JOptionPane.showInputDialog("What is the value of X in the following equation?\n\n"+"X = "+value1 + "+" +value2 +
"%" +value3 + "*" +value4 +";");
z= Integer.parseInt( zVal);
output = value1 + value2 % value3 * value4;
if (z==output)
{
JOptionPane.showMessageDialog( null,"Very Good!\nThe answer was in fact " + output,"Result",
JOptionPane.INFORMATION_MESSAGE );
++counter3;
}
else
{
JOptionPane.showMessageDialog( null,"Incorrect\nThe answer was " + output,"Result",
JOptionPane.ERROR_MESSAGE );
}