-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
50 lines (45 loc) · 1.37 KB
/
Calculator.java
File metadata and controls
50 lines (45 loc) · 1.37 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
package com.example.calculator;
import java.util.ArrayList;
import java.util.List;
//console calculator
public class Calculator {
private List<Integer> history = new ArrayList<>();
public void calculate(int first, int second, char operator) {
int result = 0;
switch (operator) {
case '+':
result = first + second;
System.out.println(result);
break;
case '-':
result = first - second;
System.out.println(result);
break;
case '*':
result = first * second;
System.out.println(result);
break;
case '/':
if (second == 0){
System.out.println("0으로 나눌 수 없음");
break;
}
result = first / second;
System.out.println(result);
break;
default:
System.out.println("잘못된 연산문자");
break;
}
history.add(result);
}
public List<Integer> getHistory() {
return history;
}
public void setHistory(List<Integer> history) {
this.history = history;
}
public void removeHistory(){
history.clear();
}
}