-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleCalculator.java
More file actions
65 lines (54 loc) · 2.04 KB
/
SimpleCalculator.java
File metadata and controls
65 lines (54 loc) · 2.04 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
package ClimateCodeCrusaders_088;
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(
"Welcome to the Simple Calculator!\n+ : Addition\n- : Subtraction\n* : Multiplication\n/ : Division");
while (true) {
double firstNumber = getInput(scanner, "Enter first number: ");
double secondNumber = getInput(scanner, "Enter second number: ");
char operator = getOperator(scanner);
double result = performOperation(firstNumber, secondNumber, operator);
System.out.println(Double.isNaN(result) ? "Error: Division by zero is not allowed."
: String.format("Result: %.1f %c %.1f = %.1f%n", firstNumber, operator, secondNumber, result));
if (!getContinueCalc(scanner))
break;
System.out.println();
}
scanner.close();
System.out.println("Thank you for using the Simple Calculator!");
}
private static double getInput(Scanner scanner, String prompt) {
while (true) {
System.out.print(prompt);
if (scanner.hasNextDouble())
return scanner.nextDouble();
System.out.println("Invalid input. Please enter a valid number.");
scanner.next(); // Clear invalid input
}
}
private static char getOperator(Scanner scanner) {
while (true) {
System.out.print("Enter an operator (+, -, *, /): ");
char operator = scanner.next().charAt(0);
if ("+-*/".indexOf(operator) != -1)
return operator;
System.out.println("Invalid operator. Please enter one of (+, -, *, /).");
}
}
private static boolean getContinueCalc(Scanner scanner) {
System.out.print("Press Enter to perform another calculation or type anything to exit: ");
scanner.nextLine(); // Consume the newline
return scanner.nextLine().isEmpty();
}
private static double performOperation(double a, double b, char op) {
return switch (op) {
case '+' -> a + b;
case '-' -> a - b;
case '*' -> a * b;
case '/' -> (b != 0) ? a / b : Double.NaN;
default -> Double.NaN;
};
}
}