-
Notifications
You must be signed in to change notification settings - Fork 54
[김예슬_BackEnd] 2주차 과제 제출합니다. #48
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
ab9e131
d451307
248bdc1
8780b02
8e3406e
4858a8c
b9a488a
8f08094
0d7140f
b6fa2a2
fa670d5
0573123
a68f7a1
9e94d5e
de5b082
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,15 @@ | ||
| 자동차 이름을 입력 | ||
|
|
||
| 입력받은 문장 쪼개기 | ||
|
|
||
| 예외처리 | ||
|
|
||
| 게임 횟수 입력 | ||
|
|
||
| 게임 진행 출력 및 랜덤 계산 | ||
|
|
||
| 우승자 찾기 | ||
|
|
||
| 우승자 출력 | ||
|
|
||
| Test추가 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,106 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import static camp.nextstep.edu.missionutils.Console.readLine; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
|
|
||
| public class Application { | ||
|
|
||
| @Test | ||
| void test() { | ||
| String input = "1,2,3,45"; | ||
| String[] result = input.split(","); | ||
|
|
||
| assertThat(result).contains("45","3","2", "1"); | ||
| assertThat(result).containsExactly("1","2","3", "45"); | ||
| } | ||
|
Comment on lines
+15
to
+22
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. 이 코드는 테스트 코드 시험 목적으로 작성하신건가요? 테스트 코드는 보통 src/test/java 패키지 내부에서 작성해요. 테스트 코드를 별도의 패키지에서 작성하는 이유가 뭘까요? |
||
|
|
||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| String userInput = readLine().trim(); | ||
| List<String> carNames = new ArrayList<>(); | ||
|
|
||
| for (String name : userInput.split(",")) { | ||
| carNames.add(name.trim()); | ||
| } | ||
|
Comment on lines
+29
to
+31
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. stream(), trim(), join() 등 자바에서 제공하는 api를 적극적으로 사용하진 부분이 좋습니다! |
||
|
|
||
| //예외처리--------------------------------------------------- | ||
| try { | ||
|
Comment on lines
+32
to
+34
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 : carNames) { | ||
| if (Name.length() > 5) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
|
Comment on lines
+36
to
+38
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.
|
||
| } | ||
| } catch (IllegalArgumentException e) { | ||
| return; | ||
| } | ||
|
Comment on lines
+40
to
+42
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. try-catch 문을 통해 예외 처리를 해주셨네요. catch 문 내부를 비워두셨는데, 이유가 있으신가요? |
||
|
|
||
| System.out.println("시도할 회수는 몇회인가요?"); | ||
| String count1 = readLine(); | ||
| int count = Integer.parseInt(count1); | ||
|
|
||
| System.out.println("실행 결과"); | ||
|
|
||
| //경주준비------------------------------------------------- | ||
| List<Integer> score = new ArrayList<>(); | ||
| String line; | ||
|
|
||
| for (String Name1 : carNames) { | ||
| score.add(0); | ||
| } | ||
|
|
||
| //경주시작------------------------------------------------- | ||
| int ssc = 0; | ||
|
Comment on lines
+58
to
+59
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 < count * carNames.size(); i++) { | ||
|
|
||
| if (ssc == carNames.size()) { | ||
| ssc = 0; | ||
| } | ||
|
|
||
| //랜덤계산------------------------------------------------- | ||
| int randomInt = Randoms.pickNumberInRange(0, 9); | ||
| if (randomInt > 3) { | ||
| score.set(ssc, score.get(ssc) + 1); | ||
|
Comment on lines
+67
to
+69
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. 요구사항에서 주어진 값을 상수로 처리하는건 어떻게 생각하시나요? |
||
| } | ||
| ssc = ssc + 1; | ||
| } | ||
|
|
||
| //1등 찾기-------------------------------------------------------------- | ||
| int rank1 = 1; | ||
| int rank2 = 0; | ||
| for (int k = 0; score.size() - 1 > k; k++) { | ||
| rank2 = Math.max(score.get(k), score.get(k + 1)); | ||
| if (rank2>rank1){ | ||
| rank1=rank2; | ||
| } | ||
| } | ||
|
Comment on lines
+75
to
+82
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. 가장 멀리 나간 자동차를 찾기 위한 로직 같아요. rank2 변수를 사용하지 않는 방법은 없을까요? |
||
|
|
||
| if (carNames.size()==1){ | ||
| rank1=score.get(0); | ||
| } | ||
|
Comment on lines
+84
to
+86
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 k = 0; score.size() > k; k++) { | ||
| line = "-".repeat(score.get(k)); | ||
| System.out.println(carNames.get(k) + " : " + line); | ||
| } | ||
| System.out.println(""); | ||
|
|
||
| // 1등 출력--------------------------------------------------- | ||
| List<String> winner = new ArrayList<>(); | ||
| for (int k = 0; score.size()> k; k++) { | ||
| if (rank1== score.get(k)){ | ||
| winner.add(carNames.get(k)); | ||
| } | ||
| } | ||
| System.out.print("최종 우승자 : " + 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.
기능 정리하고 각 기능 단위로 커밋 해주셨군요! 좋습니다!!
커밋 메시지 관련해서
깃허브 커밋 메시지 컨벤션키워드 더 공부해보시면 좋을 것 같아요.코인 프로젝트의 커밋 메시지를 참고해보세요!