-
Notifications
You must be signed in to change notification settings - Fork 49
/
CommandLineCalculator.java
44 lines (35 loc) · 1.29 KB
/
CommandLineCalculator.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
import java.util.Scanner;
public class CommandLineCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the first operand: ");
double operand1 = scanner.nextDouble();
System.out.println("Enter the operator (+, -, *, /): ");
String operator = scanner.next();
System.out.println("Enter the second operand: ");
double operand2 = scanner.nextDouble();
double result = 0.0;
switch (operator) {
case "+":
result = operand1 + operand2;
break;
case "-":
result = operand1 - operand2;
break;
case "*":
result = operand1 * operand2;
break;
case "/":
if (operand2 != 0) {
result = operand1 / operand2;
} else {
System.err.println("Error: Division by zero is not allowed.");
}
break;
default:
System.err.println("Error: Invalid operator. Please use +, -, *, or /.");
break;
}
System.out.println("Result: " + result);
}
}