Question
Hello I am wondering about JOptionpane. I'm trying to get my code to read the red X in the top right hand corner of each
Hello I am wondering about JOptionpane. I'm trying to get my code to read the red X in the top right hand corner of each dialog box. I used an if else statement at the bottom of my code where it outputs the "Add, subtract, multiply and divide." But I can't seem to get the if and else statement to work at the top of my code for all the other dialog boxes. Can you help me with this?
Here's my code:
import javax.swing.JOptionPane; public class Lab13 { public static void main(String[] args) { String name = JOptionPane.showInputDialog(null, "Please enter your name", "Name", JOptionPane.INFORMATION_MESSAGE);
double n1 = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter first decimal number", "First number", JOptionPane.INFORMATION_MESSAGE)); double n2 = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter second decimal number", "Second number", JOptionPane.INFORMATION_MESSAGE)); String[] choices = new String[] {"ADD", "SUBTRACT", "MULTIPLY", "DIVIDE"}; int num = JOptionPane.showOptionDialog(null, "Do you want to add, subtract, multiply, or divide the two numbers?", "Select one", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]); System.out.println(num); if(num == 0) { double result = n1 + n2; String input = "Hi " + name + ", " + n1 + " + " + n2 + " = " + result; JOptionPane.showMessageDialog(null, input, "Result", JOptionPane.INFORMATION_MESSAGE); } else if(num == 1) { double result = n1 - n2; String input = "Hi " + name + ", " + n1 + " - " + n2 + " = " + result; JOptionPane.showMessageDialog(null, input, "Result", JOptionPane.INFORMATION_MESSAGE); } else if(num == 2) { double result = n1 * n2; String input = "Hi " + name + ", " + n1 + " * " + n2 + " = " + result; JOptionPane.showMessageDialog(null, input, "Result", JOptionPane.INFORMATION_MESSAGE); } else if(num == 3) { double result = n1 % n2; String input = "Hi " + name + ", " + n1 + " % " + n2 + " = " + result; JOptionPane.showMessageDialog(null, input, "Result", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null, "Select any button to exit", "Error", JOptionPane.ERROR_MESSAGE); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started