-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCalculator.java
48 lines (44 loc) · 1.24 KB
/
Calculator.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
45
46
47
48
package chapter_seven.samples;
/**
* Listing 7.9 Calculator.java
*/
public class Calculator
{
/**
* Main method
*/
public static void main(String[] args)
{
// Check number of strings passed
if (args.length != 3)
{
System.out.println(
"Usage: java Calculator operand1 operator operand2");
System.exit(1);
}
// The result of the operation
int result = 0;
// Determine the operator
switch (args[1].charAt(0))
{
case '+':
result = Integer.parseInt(args[0]) +
Integer.parseInt(args[2]);
break;
case '−':
result = Integer.parseInt(args[0]) -
Integer.parseInt(args[2]);
break;
case '.':
result = Integer.parseInt(args[0]) *
Integer.parseInt(args[2]);
break;
case '/':
result = Integer.parseInt(args[0]) /
Integer.parseInt(args[2]);
}
// Display result
System.out.println(args[0] + ' ' + args[1] + ' ' + args[2]
+ " = " + result);
}
}