-
Notifications
You must be signed in to change notification settings - Fork 29
김남홍_BackEnd 1주차과제 제출 #11
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자 이상일 경우 에러발생\ | ||
| \# 빈칸일 경우 에러발생\ | ||
| 2.게임 횟수 입력받기\ | ||
| 3.0~9 사이의 값을 난수로 발생시켜 4이상일 경우 움직이게 한다.\ | ||
| 4.정해진 횟수만큼 게임 실행 후 최종 가장 많이 움직인 플레이어를 우승자로 한다.\ | ||
| \# 공동우승 가능(,로 구분하여 출력) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,79 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.*; | ||
| import java.util.Arrays; | ||
| public class Application { | ||
|
|
||
| public void forward(String[] cars){ | ||
| for (int i =0;i < cars.length; i++){ | ||
|
|
||
| } | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
|
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. main()에서 입력, 검증, 게임 진행, 우승자 출력까지 모두 담당하고 있어 역할별 메서드로 나누면 가독성이 더 좋아질 것 같습니다. |
||
| // TODO: 프로그램 구현 | ||
|
|
||
| //플레이어 자동차 이름 입력받기 | ||
| System.out.print("경주할 자동차 이름을 입력하세요 (,로 구분하고 5자 이하) : "); | ||
| String carsinput = Console.readLine(); | ||
| String[] cars = carsinput.split(","); | ||
|
|
||
| //이름 예외처리 | ||
|
|
||
| for(int i =0;i < cars.length;i++){ //5자 이하로 | ||
| if (cars[i].length() > 5){ | ||
| throw new IllegalArgumentException(); | ||
| } | ||
|
|
||
| if(cars[i].isBlank()){ //빈칸 | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
| //게임 횟수 입력 | ||
| System.out.print("게임 횟수를 입력하세요 : "); | ||
| int game = Integer.parseInt(Console.readLine()); | ||
| //횟수 예외처리 | ||
| if (game <1){ | ||
| throw new IllegalArgumentException(); | ||
| } | ||
|
|
||
| //경주 게임 출력 | ||
|
|
||
| String[] race = new String[cars.length]; | ||
| Arrays.fill(race,""); | ||
| int[] count = new int[cars.length]; | ||
|
Comment on lines
+41
to
+43
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<game;i++){ | ||
| for (int j = 0; j < cars.length; j++) { | ||
| int num = Randoms.pickNumberInRange(0, 9); | ||
| System.out.print(cars[j] + " : "); | ||
| if (num >= 4) {race[j] += "-";System.out.println(race[j]);count[j]++;} | ||
|
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. 한 줄에 여러 동작이 함께 들어가 있어, 줄을 나누면 읽기 훨씬 편해질 것 같습니다. |
||
| else System.out.println(race[j]); | ||
|
|
||
| } | ||
| System.out.println(); | ||
| } | ||
|
|
||
| //우승자 출력 | ||
| System.out.print("최종 우승자 : "); | ||
| int s = cars.length; | ||
| for (int i = 0 ; i < s ; i++){ | ||
|
|
||
| for (int j = 0; j < s; j++){ | ||
| if (j == i)continue; | ||
| if ( count[j] > count[i]){ | ||
| System.out.print(cars[j]); | ||
| break; | ||
| } | ||
| else if(count[j] == count[i]){ | ||
| System.out.print(cars[j]+","+cars[i]); | ||
| break; | ||
| } | ||
|
|
||
|
|
||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
Comment on lines
+70
to
+77
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. 불필요한 공백들은 제거해주세요:) |
||
| } | ||
| } | ||
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.
해당 코드를 남겨놓은 이유가 있을까요?