This repository was archived by the owner on Nov 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
120 lines (101 loc) · 4.58 KB
/
Main.java
File metadata and controls
120 lines (101 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
int optDummy = 0;
boolean isPresent = false;
final int[] choices = {1, 2, 3, 4, 5};
do {
final String menu = """
Enter a number:
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Exit""";
final int option = Integer.parseInt(JOptionPane.showInputDialog(menu));
optDummy = option;
for (int choice : choices) {
if (option == choice) {
isPresent = true;
}
}
switch (option) {
case 1: {
try {
final double sum = addition();
JOptionPane.showMessageDialog(null, "The sum of the numbers you entered is: " + sum, "", 1);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Oops! Try again!", "Error!", 0);
isPresent = false;
}
break;
}
case 2: {
try {
final double diff = subtraction();
JOptionPane.showMessageDialog(null, "The difference of the numbers is: " + diff, "", 1);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Oops! Try again!", "Error!", 0);
isPresent = false;
}
break;
}
case 3: {
try {
final double mult = multiplication();
JOptionPane.showMessageDialog(null, "The difference of the numbers is: " + mult, "", 1);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Oops! Try again!", "Error!", 0);
isPresent = false;
}
break;
}
case 4: {
try {
final double div = division();
JOptionPane.showMessageDialog(null, "The difference of the numbers is: " + div, "", 1);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Oops! Try again!", "Error!", 0);
isPresent = false;
}
break;
}
case 5: {
JOptionPane.showMessageDialog(null, "Exiting...", "Leaving", -1);
closeProg(0);
break;
}
default: {
JOptionPane.showMessageDialog(null, "Try again.", "Error!", 0);
}
}
} while (!isPresent);
}
private static double addition() throws Exception {
final double n1 = Double.parseDouble(JOptionPane.showInputDialog("Enter the first number: "));
final double n2 = Double.parseDouble(JOptionPane.showInputDialog(n1 + " entered.\nEnter the second number: "));
final double sum = n1 + n2;
return sum;
}
private static double subtraction() throws Exception {
final double n1 = Double.parseDouble(JOptionPane.showInputDialog("Enter the first number: "));
final double n2 = Double.parseDouble(JOptionPane.showInputDialog(n1 + " entered.\nEnter the second number: "));
final double diff = n1 - n2;
return diff;
}
private static double division() throws Exception {
final double n1 = Double.parseDouble(JOptionPane.showInputDialog("Enter the first number: "));
final double n2 = Double.parseDouble(JOptionPane.showInputDialog(n1 + " entered.\nEnter the second number: "));
final double quotient = n1 / n2;
return quotient;
}
private static double multiplication() throws Exception {
final double n1 = Double.parseDouble(JOptionPane.showInputDialog("Enter the first number: "));
final double n2 = Double.parseDouble(JOptionPane.showInputDialog(n1 + " entered.\nEnter the second number: "));
final double mult = n1 * n2;
return mult;
}
private static void closeProg(int exitCode) {
System.exit(exitCode);
}
}