From 3db7f9d9ba50c8bf1b14722a9bb77f80cfee3a77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=8F=84=EC=99=84?= Date: Sun, 29 Mar 2026 18:59:19 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=EA=B3=BC=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Application.java | 71 +++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index a17a52e..deae373 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -1,7 +1,76 @@ package racingcar; +import camp.nextstep.edu.missionutils.Randoms; +import java.util.Scanner; + public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); + Scanner sc = new Scanner(System.in); //scanner는 scanf 처럼 사용 + String[] cars = sc.nextLine().split(","); //, 를 통하여 구별 + int max = 0; //우승자 구할때 사용 + for (int i = 0; i < cars.length; i++) { + if (cars[i].isEmpty()) { + throw new IllegalArgumentException("이름은 비어 있을 수 없음"); + } + if (!cars[i].equals(cars[i].trim())) { + throw new IllegalArgumentException("이름에 공백 불가"); + } + if (cars[i].length() > 5) { + throw new IllegalArgumentException("이름은 5자 이하"); + } + for (int j = i + 1; j < cars.length; j++) { + if (cars[i].equals(cars[j])) { + throw new IllegalArgumentException("중복 이름 불가"); //IllegalArgumentException을 사용하여 조건이 맞지않으면 프로그램 종료 + } + } + } + System.out.println("시도할 회수는 몇회인가요?"); + String input = sc.nextLine(); + int tryNum; + try { + tryNum = Integer.parseInt(input); //문자열을 정수형으로 바꿈 만약 정수형으로 안바뀌면 + } catch (NumberFormatException e) { + throw new IllegalArgumentException("횟수는 숫자여야 함"); //catch에 있는 문장을 읽고 IllegalArgumentException을 통하여 프로그램 종료 + } + int[] arr = new int[cars.length]; + if (tryNum < 1) { + throw new IllegalArgumentException("횟수는 1 이상"); + } + System.out.println(); + System.out.println("실행 결과"); + for (int i = 0; i < tryNum; i++) { + for (int j = 0; j < cars.length; j++) { + int randNum = Randoms.pickNumberInRange(0, 9); //기존에 import java.util.Random을 사용하였었는데 테스트가 통과하지 않아 바꿨습니다! + if (randNum >= 4) { + arr[j] += 1; + } + System.out.print(cars[j] + " : "); //print까지 써줌으로써 이어서 -가 나오도록 합니다. + for (int k = 0; k < arr[j]; k++) { + System.out.print("-"); + } + System.out.println(); + } + System.out.println(); + } + for (int i = 0; i < arr.length; i++) { + if (arr[i] > max) { + max = arr[i]; //배열 값중 가장큰 값을 max로 저장 + } + } + System.out.print("최종 우승자 : "); + boolean first = true; + for (int i = 0; i < arr.length; i++) { + if (arr[i] == max) { + if (!first) { //ex)4,2,4 라면 처음 값이 max에 들어가고 이걸 통과한후 출력됨 이후 false 로 바뀜 + System.out.print(", "); // 2는 그냥 통과, 다시 4가 왔을때 !false는 true 이므로 , 후 출력 + } + System.out.print(cars[i]); + first = false; + } + } } } +// 자동차를 배열로 받고 시행횟수 받고 배열만들고 +// 랜덤함수 돌리고 숫자 넘으면 -를 배열에 들어간 숫자만큼 반복하고 +//비교 하고 최종우승자 나오게해보자 \ No newline at end of file From a38cf411349796ef47666e34372d8a3b48f53d26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=8F=84=EC=99=84?= Date: Thu, 2 Apr 2026 22:49:17 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=84=B0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Application.java | 41 +++++++++++++----------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index deae373..041f5e5 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -1,14 +1,16 @@ package racingcar; import camp.nextstep.edu.missionutils.Randoms; + import java.util.Scanner; public class Application { public static void main(String[] args) { System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); - Scanner sc = new Scanner(System.in); //scanner는 scanf 처럼 사용 - String[] cars = sc.nextLine().split(","); //, 를 통하여 구별 + Scanner sc = new Scanner(System.in); + String[] cars = sc.nextLine().split(","); int max = 0; //우승자 구할때 사용 + for (int i = 0; i < cars.length; i++) { if (cars[i].isEmpty()) { throw new IllegalArgumentException("이름은 비어 있을 수 없음"); @@ -21,49 +23,55 @@ public static void main(String[] args) { } for (int j = i + 1; j < cars.length; j++) { if (cars[i].equals(cars[j])) { - throw new IllegalArgumentException("중복 이름 불가"); //IllegalArgumentException을 사용하여 조건이 맞지않으면 프로그램 종료 + throw new IllegalArgumentException("중복 이름 불가"); } } } + System.out.println("시도할 회수는 몇회인가요?"); String input = sc.nextLine(); + int tryNum; try { - tryNum = Integer.parseInt(input); //문자열을 정수형으로 바꿈 만약 정수형으로 안바뀌면 + tryNum = Integer.parseInt(input); } catch (NumberFormatException e) { - throw new IllegalArgumentException("횟수는 숫자여야 함"); //catch에 있는 문장을 읽고 IllegalArgumentException을 통하여 프로그램 종료 + throw new IllegalArgumentException("횟수는 숫자여야 함"); } + int[] arr = new int[cars.length]; if (tryNum < 1) { throw new IllegalArgumentException("횟수는 1 이상"); } + System.out.println(); System.out.println("실행 결과"); + String a = "-"; + for (int i = 0; i < tryNum; i++) { for (int j = 0; j < cars.length; j++) { - int randNum = Randoms.pickNumberInRange(0, 9); //기존에 import java.util.Random을 사용하였었는데 테스트가 통과하지 않아 바꿨습니다! - if (randNum >= 4) { + + if (Randoms.pickNumberInRange(0, 9) >= 4) { arr[j] += 1; } - System.out.print(cars[j] + " : "); //print까지 써줌으로써 이어서 -가 나오도록 합니다. - for (int k = 0; k < arr[j]; k++) { - System.out.print("-"); - } - System.out.println(); + System.out.print(cars[j] + " : "); + System.out.println(a.repeat(arr[j])); } System.out.println(); } + for (int i = 0; i < arr.length; i++) { if (arr[i] > max) { - max = arr[i]; //배열 값중 가장큰 값을 max로 저장 + max = arr[i]; } } + System.out.print("최종 우승자 : "); boolean first = true; + for (int i = 0; i < arr.length; i++) { if (arr[i] == max) { - if (!first) { //ex)4,2,4 라면 처음 값이 max에 들어가고 이걸 통과한후 출력됨 이후 false 로 바뀜 - System.out.print(", "); // 2는 그냥 통과, 다시 4가 왔을때 !false는 true 이므로 , 후 출력 + if (!first) { + System.out.print(", "); } System.out.print(cars[i]); first = false; @@ -71,6 +79,3 @@ public static void main(String[] args) { } } } -// 자동차를 배열로 받고 시행횟수 받고 배열만들고 -// 랜덤함수 돌리고 숫자 넘으면 -를 배열에 들어간 숫자만큼 반복하고 -//비교 하고 최종우승자 나오게해보자 \ No newline at end of file From 79836db0813fc88f8bc2bc4d568a0a29eef5924f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=8F=84=EC=99=84?= Date: Fri, 3 Apr 2026 11:47:52 +0900 Subject: [PATCH 3/4] =?UTF-8?q?Input=20class=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Application.java | 53 +++++++++++------------- src/main/java/racingcar/Input.java | 14 +++++++ 2 files changed, 38 insertions(+), 29 deletions(-) create mode 100644 src/main/java/racingcar/Input.java diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index 041f5e5..0b0af99 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -2,67 +2,62 @@ import camp.nextstep.edu.missionutils.Randoms; -import java.util.Scanner; - public class Application { public static void main(String[] args) { - System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); - Scanner sc = new Scanner(System.in); - String[] cars = sc.nextLine().split(","); - int max = 0; //우승자 구할때 사용 + + String[] cars = Input.InputNames().split(","); + int max = 0; for (int i = 0; i < cars.length; i++) { - if (cars[i].isEmpty()) { - throw new IllegalArgumentException("이름은 비어 있을 수 없음"); + if (cars[i].isBlank()) { + throw new IllegalArgumentException("이름은 비어 있을 수 없습니다."); } if (!cars[i].equals(cars[i].trim())) { - throw new IllegalArgumentException("이름에 공백 불가"); + throw new IllegalArgumentException("이름 앞뒤 공백은 허용되지 않습니다."); + } + if (cars[i].contains(" ")) { + throw new IllegalArgumentException("이름에 공백은 포함될 수 없습니다."); } if (cars[i].length() > 5) { - throw new IllegalArgumentException("이름은 5자 이하"); + throw new IllegalArgumentException("이름은 5자 이하여야 합니다."); } + for (int j = i + 1; j < cars.length; j++) { if (cars[i].equals(cars[j])) { - throw new IllegalArgumentException("중복 이름 불가"); + throw new IllegalArgumentException("중복된 이름은 사용할 수 없습니다."); } } } - System.out.println("시도할 회수는 몇회인가요?"); - String input = sc.nextLine(); - int tryNum; try { - tryNum = Integer.parseInt(input); + tryNum = Integer.parseInt(Input.InputCount()); } catch (NumberFormatException e) { - throw new IllegalArgumentException("횟수는 숫자여야 함"); + throw new IllegalArgumentException("시도 횟수는 숫자여야 합니다."); } - int[] arr = new int[cars.length]; if (tryNum < 1) { - throw new IllegalArgumentException("횟수는 1 이상"); + throw new IllegalArgumentException("시도 횟수는 1 이상이어야 합니다."); } + int[] arr = new int[cars.length]; + System.out.println(); System.out.println("실행 결과"); - String a = "-"; for (int i = 0; i < tryNum; i++) { for (int j = 0; j < cars.length; j++) { - if (Randoms.pickNumberInRange(0, 9) >= 4) { - arr[j] += 1; + arr[j]++; } System.out.print(cars[j] + " : "); - System.out.println(a.repeat(arr[j])); + System.out.println("-".repeat(arr[j])); } System.out.println(); } for (int i = 0; i < arr.length; i++) { - if (arr[i] > max) { - max = arr[i]; - } + if (arr[i] > max) max = arr[i]; } System.out.print("최종 우승자 : "); @@ -70,12 +65,12 @@ public static void main(String[] args) { for (int i = 0; i < arr.length; i++) { if (arr[i] == max) { - if (!first) { - System.out.print(", "); - } + if (!first) System.out.print(", "); System.out.print(cars[i]); first = false; } } + + System.out.println(); } -} +} \ No newline at end of file diff --git a/src/main/java/racingcar/Input.java b/src/main/java/racingcar/Input.java new file mode 100644 index 0000000..df059ff --- /dev/null +++ b/src/main/java/racingcar/Input.java @@ -0,0 +1,14 @@ +package racingcar; + +import camp.nextstep.edu.missionutils.Console; + +public class Input { + + public static String InputNames() { + return Console.readLine(); + } + + public static String InputCount() { + return Console.readLine(); + } +} \ No newline at end of file From 7e98318089434a742e3af38d84d3a761298cb9bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=8F=84=EC=99=84?= Date: Sat, 4 Apr 2026 18:57:55 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=EC=98=88=EC=99=B8=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Application.java | 37 ++-------------------- src/main/java/racingcar/Exception.java | 40 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 34 deletions(-) create mode 100644 src/main/java/racingcar/Exception.java diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index 0b0af99..bd52571 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -7,39 +7,8 @@ public static void main(String[] args) { String[] cars = Input.InputNames().split(","); int max = 0; - - for (int i = 0; i < cars.length; i++) { - if (cars[i].isBlank()) { - throw new IllegalArgumentException("이름은 비어 있을 수 없습니다."); - } - if (!cars[i].equals(cars[i].trim())) { - throw new IllegalArgumentException("이름 앞뒤 공백은 허용되지 않습니다."); - } - if (cars[i].contains(" ")) { - throw new IllegalArgumentException("이름에 공백은 포함될 수 없습니다."); - } - if (cars[i].length() > 5) { - throw new IllegalArgumentException("이름은 5자 이하여야 합니다."); - } - - for (int j = i + 1; j < cars.length; j++) { - if (cars[i].equals(cars[j])) { - throw new IllegalArgumentException("중복된 이름은 사용할 수 없습니다."); - } - } - } - - int tryNum; - try { - tryNum = Integer.parseInt(Input.InputCount()); - } catch (NumberFormatException e) { - throw new IllegalArgumentException("시도 횟수는 숫자여야 합니다."); - } - - if (tryNum < 1) { - throw new IllegalArgumentException("시도 횟수는 1 이상이어야 합니다."); - } - + Exception.ExceptionName(cars); + int tryNum = Exception.ExceptionNum(Input.InputCount()); int[] arr = new int[cars.length]; System.out.println(); @@ -73,4 +42,4 @@ public static void main(String[] args) { System.out.println(); } -} \ No newline at end of file +} diff --git a/src/main/java/racingcar/Exception.java b/src/main/java/racingcar/Exception.java new file mode 100644 index 0000000..85e9959 --- /dev/null +++ b/src/main/java/racingcar/Exception.java @@ -0,0 +1,40 @@ +package racingcar; + +public class Exception { + public static void ExceptionName(String[] cars){ + + for (int i = 0; i < cars.length; i++) { + if (cars[i].isBlank()) { + throw new IllegalArgumentException("이름은 비어 있을 수 없습니다."); + } + if (!cars[i].equals(cars[i].trim())) { + throw new IllegalArgumentException("이름 앞뒤 공백은 허용되지 않습니다."); + } + if (cars[i].contains(" ")) { + throw new IllegalArgumentException("이름에 공백은 포함될 수 없습니다."); + } + if (cars[i].length() > 5) { + throw new IllegalArgumentException("이름은 5자 이하여야 합니다."); + } + + for (int j = i + 1; j < cars.length; j++) { + if (cars[i].equals(cars[j])) { + throw new IllegalArgumentException("중복된 이름은 사용할 수 없습니다."); + } + } + } + } + public static int ExceptionNum(String input) { + try { + int tryNum = Integer.parseInt(input); + + if (tryNum < 1) { + throw new IllegalArgumentException("시도 횟수는 1 이상이어야 합니다."); + } + + return tryNum; + } catch (NumberFormatException e) { + throw new IllegalArgumentException("시도 횟수는 숫자여야 합니다."); + } + } +}