-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
33 lines (28 loc) · 1.39 KB
/
Calculator.java
File metadata and controls
33 lines (28 loc) · 1.39 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
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) { // Use try-with-resources to avoid resource leaks
System.out.println("Enter the value of a: ");
double a = sc.nextDouble();
System.out.println("Enter the value of b: ");
double b = sc.nextDouble();
System.out.println("Choose an operation: ");
System.out.println("1: + (Addition)");
System.out.println("2: - (Subtraction)");
System.out.println("3: * (Multiplication)");
System.out.println("4: / (Division)");
System.out.println("5: % (Modulo)");
int operation = sc.nextInt();
// Rule switch introduced in Java 14 (standard in Java 17)
String result = switch (operation) {
case 1 -> "Result: " + (a + b);
case 2 -> "Result: " + (a - b);
case 3 -> "Result: " + (a * b);
case 4 -> (b != 0) ? "Result: " + (a / b) : "Error: Division by zero is not allowed.";
case 5 -> (b != 0) ? "Result: " + (a % b) : "Error: Division by zero is not allowed.";
default -> "Invalid operation selected.";
};
System.out.println(result); // Print the result or error
}
}
}