-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculatorTryCatch.java
More file actions
52 lines (41 loc) · 1.57 KB
/
Copy pathCalculatorTryCatch.java
File metadata and controls
52 lines (41 loc) · 1.57 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
import java.util.Scanner;
public class CalculatorTryCatch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("*** Simple Calculator ***");
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();
System.out.println("\nChoose an operation:");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.print("Enter your choice (1-4): ");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Result: " + (a + b));
break;
case 2:
System.out.println("Result: " + (a - b));
break;
case 3:
System.out.println("Result: " + (a * b));
break;
case 4:
try {
int result = a / b;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: Cannot divide by zero!");
}
break;
default:
System.out.println("Invalid choice! Please choose between 1 to 4.");
}
sc.close();
System.out.println("\nCalculation completed. Thank you!");
}
}