-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathApplication.java
More file actions
63 lines (52 loc) · 1.99 KB
/
Application.java
File metadata and controls
63 lines (52 loc) · 1.99 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
51
52
53
54
55
56
57
58
59
60
61
62
63
package racingcar;
import camp.nextstep.edu.missionutils.Console;
import camp.nextstep.edu.missionutils.Randoms;
public class Application {
public static void main(String[] args) {
System.out.print("자동차 이름들을 ,를 사용해 입력하세요(이름은 5자 이하): ");
String inputNames = Console.readLine();
String[] carNames = inputNames.split(",");
for (int i = 0; i < carNames.length; i++) {
carNames[i] = carNames[i].trim();
if (carNames[i].isEmpty() || carNames[i].length() > 5) {
throw new IllegalArgumentException("자동차 이름은 5자 이내여야 합니다.");
}
}
System.out.print("시도 횟수 : ");
String tryCountInput = Console.readLine();
int tryCount = Integer.parseInt(tryCountInput);
if (tryCount <= 0) {
throw new IllegalArgumentException("잘못된 값입니다.");
}
int[] positions = new int[carNames.length];
System.out.println("\n실행 결과");
for (int i = 0; i < tryCount; i++) {
for (int j = 0; j < carNames.length; j++) {
int num = Randoms.pickNumberInRange(0, 9);
if (num >= 4) {
positions[j]++;
}
System.out.println(carNames[j] + " : " + "-".repeat(positions[j]));
}
System.out.println();
}
int maxDistance = 0;
for (int pos : positions) {
if (pos > maxDistance) {
maxDistance = pos;
}
}
// 우승자 문자열 만들기
String winners = "";
for (int i = 0; i < carNames.length; i++) {
if (positions[i] == maxDistance) {
if (!winners.isEmpty()) {
winners += ",";
}
winners += carNames[i];
}
}
// 우승자 출력
System.out.println("최종 우승자 : " + winners);
}
}