Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# 기능 요구사항 정리
## 1. 입력기능
- 자동차 이릅 입력받기
- 시도할 횟수 입력받기
- 입력받은 자동차 이름이 5자 이하인지 확인하기
- 숫자에 따라 자동차가 전진/후진 할지 결정하기
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

어랏 후진도 있었나요??

- 최종 우승자 출력하기

102 changes: 99 additions & 3 deletions src/main/java/racingcar/Application.java
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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (names[i].isEmpty()) // 빈값 (예: pobi,,woni)
{
throw new IllegalArgumentException();
}
if (names[i].contains(" ")) // 이름에 공백 포함 (예: pobi, woni)
{
throw new IllegalArgumentException();
}
if (names[i].isBlank())
{
throw new IllegalArgumentException();
}

이런 식으로 통합할 수도 있어 보여요!
isEmptyisBlank의 차이를 찾아보시는걸 추천드립니다.

}

// 2. 이름 중복 예외 처리 (이중 for문으로 하나씩 비교)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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];
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 이러면 자동차의 이름과 위치를 담당하는 배열이 분리되어 있으니 이를 하나로 묶는게 낫지 않을까요?

  2. 어떻게 묶을까요?


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);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3번 라인처럼 import를 사용하면 랜덤 값을 꺼내기 위한 메소드를 사용하는데 이렇게 긴 코드가 필요하지 않을거에요

if (randomnum >= 4)
Comment on lines +66 to +67
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int randomnum = camp.nextstep.edu.missionutils.Randoms.pickNumberInRange(0,9);
if (randomnum >= 4)
if (pickNumberInRange(0,9) >= 4)
  1. 바로 이런 식으로 한다면 randomnum이라는 변수는 필요없지 않을까요?

  2. 자바에서는 변수명을 카멜케이스로 사용하는 것이 컨벤션 입니다. 자바의 네이밍 컨벤션에 대해서도 찾아보시는걸 추천드려요!

{
position[j]++;
}

System.out.print(names[j] + " : "); // 콜론 앞뒤 공백 수정
for(int k = 0; k < position[j]; k++)
{
System.out.print("-");
}
Comment on lines +73 to +76
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String.repeat() 메소드를 사용한다면 반복문이 필요없어져요!

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)); // 결과 출력
}
}
}