-
Notifications
You must be signed in to change notification settings - Fork 1
/
SimpleCalculator.java
executable file
·56 lines (48 loc) · 1.13 KB
/
SimpleCalculator.java
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
// TODO: comment this program
public class SimpleCalculator extends ConsoleProgram {
public static void main(String [] args) {
SimpleCalculator calc = new SimpleCalculator();
calc.run();
}
public void run() {
boolean done = false;
while (!done) {
print("Enter your first number: ");
int num1 = readInt();
print("Enter your second number: ");
int num2 = readInt();
long answer = 0;
boolean validInput=false;
while (!validInput) {
print("Enter your operation (+, -, /, *): ");
String operation = readLine();
switch (operation) {
case "+":
// add
answer = num1 + num2;
validInput=true;
break;
case "-":
// subtract
answer = num1 - num2;
validInput=true;
break;
case "/":
// divide
answer = num1 / num2;
validInput=true;
break;
case "*":
// multiply
answer = num1 * num2;
validInput=true;
break;
default:
println("Invalid input: try again");
}
}
System.out.println("Answer is: " + answer);
done = true;
}
}
}