-
Notifications
You must be signed in to change notification settings - Fork 8
[자동차경주] 이성준 미션 제출합니다. #7
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,11 @@ | ||
| [33mcommit 9ee2c3533fc9409283a740bc00936d7c0b52e9b4[m[33m ([m[1;36mHEAD -> [m[1;32mszoon2426[m[33m)[m | ||
| Author: szoon2426 <s.zoon2426@gmail.com> | ||
| Date: Fri Feb 14 23:19:54 2025 +0900 | ||
|
|
||
| 기본 기능 추가 | ||
|
|
||
| [33mcommit b9d9f2b0e3858ba8e98b53882eb49e7ae8cdd248[m[33m ([m[1;31morigin/main[m[33m, [m[1;31morigin/HEAD[m[33m, [m[1;32mmain[m[33m)[m | ||
| Author: moongua404 <hogun.soongit@gmail.com> | ||
| Date: Mon Jan 6 01:19:25 2025 +0900 | ||
|
|
||
| feat: project-setup |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| # java-racingcar-precourse | ||
| ## 추가할 목록 | ||
| - 게임에 참여할 참가자는 모두 각자 자동차가 있고 이름이 있다. 이름은 ,로 구분한다. 레이싱 후 결과를 알려주고 승자를 알린다. 승자가 여러명 일 경우 ,로 구분한다. 레이싱을 여러번 할 수 있도록 시도할 횟수를 입력받는다. | ||
| - 사용자가 잘못된 값을 입력할 경우 IllegalArgumentException을 발생시킨 후 애플리케이션은 종료되어야 한다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,79 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
| import camp.nextstep.edu.missionutils.Console; | ||
| import java.util.Vector; | ||
| import java.util.stream.Collectors; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| public class Application { | ||
| static Vector<Player> listOfPlayers = new Vector<>(); | ||
|
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. vector대신 arraylist를 활용하는 것은 어떨까용?? 불필요한 동기화처리도 줄이고 성능적으로도 더 적절해요! |
||
|
|
||
| // 레이싱 돌려잇 | ||
| static void game(){ | ||
| for(int i = 0; i<listOfPlayers.size(); i++){ | ||
| if(Randoms.pickNumberInRange(0,9) >=4){ | ||
| listOfPlayers.get(i).addDistance(); | ||
| } | ||
|
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. listofPlayers에 대해서 for문을 돌리시는 것 같은데, 만약 내부의 인스턴스를 사용하실거면 .forEach()를 찾아보고 그걸 쓰는 방향으로 고치고 써보시면 더 좋을 것 같습니다! 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. 동의합니다 |
||
| } | ||
| } | ||
|
|
||
| // 게임 결과 | ||
| static String getGameResult(int player){ | ||
|
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. player을 매개변수로 받기보다는 player객체 자체를 받아서 출력하는 방향이 더 좋을 듯 해용 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. 동의합니다~~ 이러면 내부 로직의 함수 호출을 한번으로 줄일 수 있을 듯 싶습니다~~ |
||
| return (listOfPlayers.get(player).getName() + " : " | ||
| + listOfPlayers.get(player).printDistance()); | ||
| } | ||
|
|
||
| // 승자의 거리 | ||
| static int distanceOfWinner(){ | ||
| int answer = 0; | ||
| for(int i = 0; i<listOfPlayers.size();i++){ | ||
| if(listOfPlayers.get(i).getDistance() > answer){ | ||
| answer = listOfPlayers.get(i).getDistance(); | ||
| } | ||
| } | ||
| return answer; | ||
| } | ||
|
|
||
| // 승자 출력 | ||
| static String printWinner(){ | ||
| int value = distanceOfWinner(); | ||
| Vector<String> listOfWinners = new Vector<>(); | ||
| for(Player player : listOfPlayers){ | ||
| if(player.getDistance() == value){ | ||
| listOfWinners.add(player.getName()); | ||
| } | ||
| } | ||
|
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 문도 가능하다면 stream을 이용하면 코드가 더 간략해지고 이해하기 쉬울 것 같아요! 대신 한 번 열린 stream은 재사용이 불가하니 한 번 해당 내용에 대해 알아보시는 것도 좋을 것 같네요, |
||
| return listOfWinners.stream().collect(Collectors.joining(", ")); | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
|
|
||
| // input | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| String names = Console.readLine(); | ||
| System.out.println("시도할 횟수는 몇회인가요?"); | ||
| int times = Integer.parseInt(Console.readLine()); | ||
|
|
||
|
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. 만약 이렇게 하시면 .parseInt부분에서 미션 요구조건의 예외 타입과 다른 예외가 나올 수도 있을 것 같습니다! 잘못된 입력에 따라 IllegalExpression이었나..아무튼 이 에러를 throw하게 고치시면 더 좋을 것 같습니다~~ 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. 동의합니당 |
||
| // 입력받은 이름들을 player 리스트에 담아줌 | ||
|
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(String name : names.split(",")){ | ||
| if(name.length()>5){ // 예외처리 | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| Player player = new Player((name)); | ||
| listOfPlayers.add(player); | ||
| } | ||
|
|
||
| // output | ||
| System.out.println("\n실행 결과"); | ||
| for(int i = 0; i<times; i++){ | ||
| game(); | ||
| for(int j = 0; j<listOfPlayers.size(); j++){ | ||
| System.out.println(getGameResult(j));; | ||
| } | ||
| System.out.println(); | ||
| }; | ||
| System.out.printf("최종 우승자 : " + printWinner()); | ||
|
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. 단순 문자열 출력이라 포맷팅이 필요하지않아보이는데 printf를 쓰신 이유가 있을까용??
Author
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. 어,, 잘 모르고 그냥 사용했는데 printf의 의미를 더 알아보고 사용해보도록 하겠습니다!!! |
||
| } | ||
| } | ||
| } | ||
|
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. 주석으로 설명을 붙인 메서드들을 다른 폴더들에 분리해서 작성하고 정리하는 게 프로젝트 파일을 보는데 더 편할 것 같아요! 한 번 시간 나실 때 MVC 패턴에 대해 검색해 보세요. 저도 처음 코딩할 땐 굳이? 싶었지만 직접 해보니 코드 작성이 더 깔끔해지는 것 같아요! |
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,30 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package racingcar; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public class Player { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private String name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private int distance; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Player(){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.name = ""; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.distance = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Player(String name){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.name = name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.distance = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+6
to
+13
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
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 commentThe reason will be displayed to describe this comment to others. Learn more. 확실히 저렇게 수정하는게 실수도 줄이고 읽고 이해하기 편해 보이네요. 저도 한 수 배우고 갑니다..! |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void addDistance(){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| distance++; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String getName(){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String printDistance(){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String answer = ""; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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.
answer을 ""로 선언하고 answer += "-"; 을 반복하는 방식은 문자열이 계속 생성되기때문에 성능저하가 일어날 수 있대용 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<distance;i++){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| answer += "-"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return answer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int getDistance(){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return distance; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
거의 대부분 static을 쓰셨는데 그 이유가 있으실까용?