i need to have a menu using joptionpane() and ask the user to choose addition ,
ID: 3645897 • Letter: I
Question
i need to have a menu using joptionpane() and ask the user to choose addition , subtraction, or multiplication, and also if they answer incorrectly then it repeats the questionthe code below
//import class JOptionPane
import javax.swing.JOptionPane;
//import class Scanner
import java.util.Scanner;
//main function
public class MultiplyNew {
static int x , sum , y , sub , mul , div;
public static void main( String args[] )
{
//declare string, questions & sumIn
//variables
String question,
sumIn;
//declare answer, sums, number1 & -
//number2 variables as integers
int answer, sums, number1, number2;
//initialize answer & sums variable
answer = 0;
sums = 0;
//get two random numbers between 0 and 9
number1 = ( int ) ( Math.random() * 10 );
number2 = ( int ) ( Math.random() * 10 );
//declare option variable and prompt
//if they want to run the program again
int option = JOptionPane.YES_OPTION;
//loop validation to re-run the program
//or to exit
while (option == JOptionPane.YES_OPTION)
{
switch
//Prompt for user input
sumIn = JOptionPane.showInputDialog
( "How much is " + number1 +
" times " + number2);
//Answer calculation
answer = number1 * number2;
//convert String to integer
sums = Integer.parseInt( sumIn );
//loop validation for correct answer
while (sums != answer) {
//If answer is incorrect inform the user
//and prompt for new answer input
sumIn = JOptionPane.showInputDialog
("Incorrect answer, please try again "
+ number1 + " times " + number2);
sums = Integer.parseInt( sumIn );
}
//If answer is correct inform the user
//and proceed to the next line of the code
JOptionPane.showMessageDialog
(null, "Very Good! Correct answer: "
+ number1 + " times " + number2+ " = " + sums);
{
}
//Ask the user if he/she wants to run the
//program again
option = JOptionPane.showConfirmDialog
(null, "Do you want to run the program again?");
//Generate new random numbers for new run
number1 = ( int ) ( Math.random() * 10 );
number2 = ( int ) ( Math.random() * 10 );
}
//Inform user they choose to exit the program
JOptionPane.showMessageDialog
(null, "You choose to exit the program, bye!");
}
//Methods to calculate addition, subtraction,
//Multiplication, and division
/////////////////////////////////////////////
public static int sum ( )
{
sum = x + y;
System.out.println("The sum is :" + sum);
return sum;
}
public static int subtract ( )
{
sub = x - y;
System.out.println("The subtract is :" + sub);
return sub;
}
public static int multiply ( )
{
mul = x * y;
System.out.println("The multiple is :" + mul);
return mul;
}
public static int divide ( )
{
div = x / y;
System.out.println("The divide is :" + div);
return div;
}
/////////////////////////////////////////////
}
Explanation / Answer
I don't see System.exit() in your code... u need to have this with JOptionPane. I don't have eclipse now so can't correct your code but can give you another code which might help you.. //hope this helps import javax.swing.JOptionPane; public class testing { public static void main(String[] args) { String[] choices = {"Addition", "Subtraction", "Multiplication", "Division"}; int jeff; int num1,num2,sum,difference,product,quotient; do { int response = JOptionPane.showOptionDialog( null , "Choose Arithmetic Operation" , "Operators" , JOptionPane.YES_NO_OPTION , JOptionPane.PLAIN_MESSAGE , null , choices , "()" ); num1 = Integer.parseInt(JOptionPane.showInputDialog("Enter First Number:")); System.out.println(num1); num2 = Integer.parseInt(JOptionPane.showInputDialog("Enter Second Number:")); System.out.println(num2); sum = num1+num2; difference = num1-num2; product = num1*num2; quotient = num1/num2; int result = (int)(Math.random() * 4); switch (result) { case 0: JOptionPane.showMessageDialog(null, "The Sum is = " +sum); break; case 1: JOptionPane.showMessageDialog(null, "The Difference is = " +difference); break; case 2: JOptionPane.showMessageDialog(null, "The Product is = " +product); break; case 3: JOptionPane.showMessageDialog(null, "The Quotient is = " +quotient); break; default: JOptionPane.showMessageDialog(null, "Invalid Input"); } jeff = JOptionPane.showConfirmDialog(null, "Try Again?"); } while (jeff == JOptionPane.YES_OPTION); System.exit(0); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.