-
Notifications
You must be signed in to change notification settings - Fork 29
[송정민_BackEnd]1주차 과제 제출합니다. #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # 기능 요구사항 정리 | ||
| ## 1. 입력기능 | ||
| - 자동차 이릅 입력받기 | ||
| - 시도할 횟수 입력받기 | ||
| - 입력받은 자동차 이름이 5자 이하인지 확인하기 | ||
| - 숫자에 따라 자동차가 전진/후진 할지 결정하기 | ||
| - 최종 우승자 출력하기 | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,7 +1,103 @@ | ||||||||||||||||||||||||||
| package racingcar; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| import camp.nextstep.edu.missionutils.Console; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| public class Application { | ||||||||||||||||||||||||||
| public static void main(String[] args) { | ||||||||||||||||||||||||||
| // TODO: 프로그램 구현 | ||||||||||||||||||||||||||
| public static void main(String[] args) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||||||||||||||||||||||||||
| String nameinput = Console.readLine(); | ||||||||||||||||||||||||||
| String[] names = nameinput.split(","); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // 1. 이름 관련 예외 처리 (빈값, 공백 포함, 5자 초과) | ||||||||||||||||||||||||||
| for (int i = 0 ; i < names.length ; i++) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| if (names[i].length() > 5) // 5자 초과 | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| throw new IllegalArgumentException(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| if (names[i].isEmpty()) // 빈값 (예: pobi,,woni) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| throw new IllegalArgumentException(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| if (names[i].contains(" ")) // 이름에 공백 포함 (예: pobi, woni) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| throw new IllegalArgumentException(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
+19
to
+26
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
이런 식으로 통합할 수도 있어 보여요! |
||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // 2. 이름 중복 예외 처리 (이중 for문으로 하나씩 비교) | ||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 컬렉션 중에 중복을 허용하지 않는 자료구조가 있습니다! |
||||||||||||||||||||||||||
| for (int i = 0; i < names.length; i++) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| for (int j = i + 1; j < names.length; j++) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| if (names[i].equals(names[j])) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| throw new IllegalArgumentException(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| System.out.println("시도할 회수는 몇회인가요?"); // 요구사항에 맞춰 '회수는'으로 수정 | ||||||||||||||||||||||||||
| String countinput = Console.readLine(); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // 3. 횟수 관련 예외 처리 (숫자가 아님, 0 이하) | ||||||||||||||||||||||||||
| int count; | ||||||||||||||||||||||||||
| try | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| count = Integer.parseInt(countinput); | ||||||||||||||||||||||||||
| if (count <= 0) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| throw new IllegalArgumentException(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| catch (NumberFormatException e) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| throw new IllegalArgumentException(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| int[] position = new int [names.length]; | ||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| System.out.println("\n실행 결과"); // '실행 결과' 띄어쓰기 수정 (테스트 통과용) | ||||||||||||||||||||||||||
| for(int i = 0; i < count; i++) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| for (int j = 0 ; j < names.length; j++) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| int randomnum = camp.nextstep.edu.missionutils.Randoms.pickNumberInRange(0,9); | ||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3번 라인처럼 |
||||||||||||||||||||||||||
| if (randomnum >= 4) | ||||||||||||||||||||||||||
|
Comment on lines
+66
to
+67
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| position[j]++; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| System.out.print(names[j] + " : "); // 콜론 앞뒤 공백 수정 | ||||||||||||||||||||||||||
| for(int k = 0; k < position[j]; k++) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| System.out.print("-"); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
+73
to
+76
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||||||||||
| System.out.println(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| System.out.println(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| int distance = 0; | ||||||||||||||||||||||||||
| for(int i = 0 ; i < position.length; i++) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| if(position[i] > distance) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| distance = position[i]; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| java.util.List<String> winner = new java.util.ArrayList<>(); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| for (int i = 0 ; i < names.length; i++) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| if (position[i] == distance) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| winner.add(names[i]); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| System.out.println("최종 우승자 : " + String.join(", ", winner)); // 결과 출력 | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
어랏 후진도 있었나요??