-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
111 lines (87 loc) · 3.15 KB
/
Main.java
File metadata and controls
111 lines (87 loc) · 3.15 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
import java.util.Scanner;
import java.util.InputMismatchException;
public class Main {
static String useSymbol = "";
static Integer number1 = 0;
static Integer number2 = 0;
public static void exitCalculator() {
System.out.println("\nClosing the calculator, have a nice day.\n");
System.exit(0);
}
public static void doOperation() {
Scanner scanner = new Scanner(System.in);
Integer answer = 0;
System.out.println("\nPlease enter two numbers for your operation:");
try {
System.out.print("[Number 1]: ");
number1 = scanner.nextInt();
System.out.print("[Number 2]: ");
number2 = scanner.nextInt();
} catch (InputMismatchException e) {
System.out.println("\nError - InputMismatchException.");
return;
}
System.out.println("Start of calculations...");
if (useSymbol.equals("+")) {
answer = number1 + number2;
} else if (useSymbol.equals("-")) {
answer = number1 - number2;
} else if (useSymbol.equals("*")) {
answer = number1 * number2;
} else if (useSymbol.equals("/")) {
answer = number1 / number2;
}
System.out.println("The correct answer for this operation is: " + answer);
}
public static void signSelector() {
Scanner scanner = new Scanner(System.in);
String signMenu = """
\n=== [ S Y M B O L S] ===
Please select a valid symbol below for your operation:
+ ........................ Addition.
- ........................ Subtraction.
* ........................ Multiplication.
/ ........................ Division.""";
System.out.println(signMenu);
System.out.print("[?]: ");
String optionTaken = scanner.next();
if (optionTaken.equals("+")) {
useSymbol = "+";
doOperation();
} else if (optionTaken.equals("-")) {
useSymbol = "-";
doOperation();
} else if (optionTaken.equals("*")) {
useSymbol = "*";
doOperation();
} else if (optionTaken.equals("/")) {
useSymbol = "/";
doOperation();
} else {
System.out.println("\nError - invalidOption: Your selected option is invalid.");
useSymbol = "";
}
}
public static void displayMenu() {
Scanner scanner = new Scanner(System.in);
String menu = """
\n=== [ M E N U ] ===
exit ..................... End this process.
start .................... Start an operation.
NOTE:
- If a number is too large, it might overflow and give wrong answers. Numbers must be integers!""";
System.out.println(menu);
System.out.print("[?]: ");
String optionTaken = scanner.next();
if (optionTaken.equals("exit")) {
exitCalculator();
} else if (optionTaken.equals("start")) {
signSelector();
}
}
public static void main(String[] args) {
while (true) {
displayMenu();
}
}
}