-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathReferee.java
More file actions
45 lines (41 loc) · 1.29 KB
/
Referee.java
File metadata and controls
45 lines (41 loc) · 1.29 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
package baseball;
import java.util.ArrayList;
import java.util.List;
public class Referee {
public int[] judge(String target, String input) {
return new int[] {countStrike(convert(target), convert(input)), countBall(convert(target), convert(input))};
}
private List<Integer> convert(String string) {
List<Integer> career = new ArrayList<>();
for(String number: string.split("")) {
career.add(Integer.parseInt(number));
}
return career;
}
private int countBall(List<Integer> target, List<Integer> input) {
int ball = 0;
if (target.get(0) == input.get(1) || target.get(0) == input.get(2)) {
ball++;
}
if (target.get(1) == input.get(0) || target.get(1) == input.get(2)) {
ball++;
}
if (target.get(2) == input.get(0) || target.get(2) == input.get(1)) {
ball++;
}
return ball;
}
private int countStrike(List<Integer> target, List<Integer> input) {
int strike = 0;
if (target.get(0) == input.get(0)) {
strike ++;
}
if (target.get(1) == input.get(1)) {
strike ++;
}
if (target.get(2) == input.get(2)) {
strike ++;
}
return strike;
}
}