-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommandLineDemo.java
More file actions
40 lines (37 loc) · 1.21 KB
/
CommandLineDemo.java
File metadata and controls
40 lines (37 loc) · 1.21 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
// File: Calculator.java
public class CommandLineDemo {
public static void main(String[] args) {
if (args.length != 3) {
System.out.println("Usage: java Calculator <num1> <operator> <num2>");
System.out.println("Example: java Calculator 10 + 5");
return;
}
double num1 = Double.parseDouble(args[0]);
double num2 = Double.parseDouble(args[2]);
String operator = args[1];
double result = 0;
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
if (num2 != 0) {
result = num1 / num2;
} else {
System.out.println("Error: Division by zero!");
return;
}
break;
default:
System.out.println("Error: Invalid operator. Use +, -, *, or /");
return;
}
System.out.println("Result: " + result);
}
}