-
Notifications
You must be signed in to change notification settings - Fork 4
[자동차 경주] 박시영 미션 제출합니다. #2
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 10 commits
7bfb034
906e134
249d53f
cc79944
b621f47
3405b05
46f9f20
1966802
df2a5a2
7e93bb7
858e14a
06a7da1
c5b3d53
468d82b
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,16 @@ | ||
| # 기능 명세서 | ||
|
|
||
| ## | ||
|
|
||
| 경주할 자동차 이름을 쉼표로 구분해 입력받음 | ||
| 5자 이하가 아닐 시 에러발생 | ||
|
|
||
| 사용자가 몇 번의 이동을 할 것인지 입력받음 | ||
| 숫자가 아닌걸 입력하면 에러 발생 | ||
|
|
||
| 입력받은 이동 횟수만큼 반복 | ||
| 각 자동차마다 0부터 9까지 무작위 숫자를 random()으로 뽑아서 5가 넘을경우 한칸 앞으로 | ||
| 현재 각각 자동차가 얼마나 앞으로 갔는지 출력 | ||
|
|
||
| 최종 우승자가 여러명이면 쉼표로 구분해 출력 아니면 그냥 출력 | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| import java.util.Random; | ||
| import java.util.StringTokenizer; | ||
|
|
||
| public class Model { | ||
| String[] car; | ||
| int[] len; | ||
| int numberOfAttempts; | ||
| int max = 0; | ||
|
|
||
| void saveCarName(String input){ | ||
|
|
||
| StringTokenizer st = new StringTokenizer(input, ","); | ||
| car = new String[st.countTokens()]; | ||
| len = new int[st.countTokens()]; | ||
| for(int i=0; i<car.length; i++){ | ||
| String str = st.nextToken(); | ||
| if(str.length()>5) throw new IllegalArgumentException("5글자 이하로 작성"); | ||
| car[i] = str; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| void saveNumberOfAttempts(String input){ | ||
|
|
||
| try { | ||
| numberOfAttempts = Integer.parseInt(input); | ||
| } catch (Exception e){ | ||
| System.out.println("숫자를 입력하세요"); | ||
| } | ||
|
Comment on lines
+31
to
+35
Contributor
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. 여기서는 아스키 코드를 이용해서 숫자 범위 안에 드는지 검사할 수 있을 듯! 근데 간단하게 짜려면 이렇게 짜도 되긴 합니당 |
||
|
|
||
| } | ||
|
|
||
| StringBuilder race(){ | ||
| StringBuilder sb = new StringBuilder(); | ||
| for (int i = 0; i < car.length; i++) { | ||
| int rand = Randoms.pickNumberInRange(0, 9); | ||
| if(rand>=4) len[i]++; | ||
| sb.append(car[i]).append(" : ") | ||
| .append("-".repeat(Math.max(0, len[i]))) | ||
| .append("\n"); | ||
| max = Math.max(len[i], max); | ||
|
||
| } | ||
|
|
||
| return sb; | ||
| } | ||
|
|
||
| StringBuilder findWinner(){ | ||
| StringBuilder sb = new StringBuilder(); | ||
| boolean jointWinner = false; | ||
| for(int i=0; i<car.length; i++){ | ||
| if(len[i]==max) { | ||
| if(jointWinner) sb.append(", "); | ||
| jointWinner = true; | ||
| sb.append(car[i]); | ||
| } | ||
| } | ||
| return sb; | ||
| } | ||
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| public class View { | ||
|
|
||
| String inputCarName(){ | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,)기준으로 구분)"); | ||
| return Console.readLine(); | ||
| } | ||
|
|
||
| String inputNumberOfAttempts(){ | ||
| System.out.println("시도할 회수는 몇회인가요?"); | ||
| return Console.readLine(); | ||
| } | ||
|
|
||
| void printResult(Model model){ | ||
| System.out.println("\n실행 결과"); | ||
| while (model.numberOfAttempts --> 0) { | ||
| System.out.println(model.race()); | ||
| } | ||
| } | ||
|
||
|
|
||
| void printWinner(Model model){ | ||
| System.out.print("최종 우승자 : "); | ||
| System.out.println(model.findWinner()); | ||
| } | ||
|
|
||
| } | ||
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.
사소한 거긴 한데 if 문은 한 줄이라도 괄호를 쳐주는 게 좋긴 합니다!