This repository was archived by the owner on Jun 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathSimpleCalculator.java
More file actions
60 lines (51 loc) · 1.83 KB
/
SimpleCalculator.java
File metadata and controls
60 lines (51 loc) · 1.83 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
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Displaying the menu
System.out.println("Simple Calculator");
System.out.println("Select an operation:");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
// Reading the user's choice
System.out.print("Enter your choice (1-4): ");
int choice = scanner.nextInt();
// Reading the two numbers
System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter second number: ");
double num2 = scanner.nextDouble();
double result = 0;
boolean validChoice = true;
// Performing the chosen operation
switch (choice) {
case 1:
result = num1 + num2;
break;
case 2:
result = num1 - num2;
break;
case 3:
result = num1 * num2;
break;
case 4:
if (num2 != 0) {
result = num1 / num2;
} else {
System.out.println("Error: Division by zero is not allowed.");
validChoice = false;
}
break;
default:
System.out.println("Invalid choice! Please select a valid operation (1-4).");
validChoice = false;
}
// Displaying the result
if (validChoice) {
System.out.println("The result is: " + result);
}
scanner.close();
}
}