-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
49 lines (42 loc) · 1.17 KB
/
Calculator.java
File metadata and controls
49 lines (42 loc) · 1.17 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
package kr.hs.dgsw.ex1;
import java.util.Scanner;
public class Calculator {
private final String operator;
private final Scanner scanner;
public Calculator(String operator) {
this.operator = operator;
this.scanner = new Scanner(System.in);
}
public int calculate(int operand1, int operand2) {
if("+".equals(operator)) {
return operand1 + operand2;
}else if("-".equals(operator)) {
return operand1 + operand2;
}else if("*".equals(operator)) {
return operand1 * operand2;
}else if("/".equals(operator)) {
return operand1 / operand2;
}else if("%".equals(operator)) {
return operand1 % operand2;
}else {
throw new RuntimeException("Unknown operator");
}
}
public void execute() {
while(true) {
System.out.println("정수를 두 개 입력하세요.");
int operand1 = scanner.nextInt();
int operand2 = scanner.nextInt();
if(operand1 == 0 && operand2 == 0) {
break;
}
int result = calculate(operand1, operand2);
System.out.printf("%d %s %d = %d", operand1, operator, operand2, result);
}
scanner.close();
}
public static void main(String[] args) {
Calculator cal = new Calculator("+");
cal.execute();
}
}