-
Notifications
You must be signed in to change notification settings - Fork 55
[박준서_BackEnd]과제 제출합니다. #59
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
Open
joonseo1016
wants to merge
1
commit into
BCSDLab-Edu:main
Choose a base branch
from
joonseo1016:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,104 @@ | ||
| package racingcar; | ||
| import java.util.Scanner; | ||
| import java.util.List; | ||
| import java.util.ArrayList; | ||
| import java.util.Random; | ||
|
|
||
| class Car{ | ||
| private final String name; | ||
| private int position = 0; | ||
| //차 이름 | ||
| public Car(String name){ | ||
| if (name == null || name.trim().isEmpty() || name.length() >5){ | ||
| throw new IllegalArgumentException("자동차 이름은 1자 이상 5자 이하만 가능합니다."); | ||
| } | ||
| this.name = name.trim(); | ||
| } | ||
| //이동 | ||
| public void move(){ | ||
| Random random = new Random(); | ||
| if (random.nextInt(10)>=4){ | ||
| this.position++; | ||
| } | ||
| } | ||
| //이름 입력 | ||
| public String getname(){ | ||
| return name; | ||
| } | ||
| //위치 | ||
| public int getPosition(){ | ||
| return position; | ||
| } | ||
| //현 상태 | ||
| public String getstatus(){ | ||
| StringBuilder status = new StringBuilder(name + " : "); | ||
| for (int i = 0; i < position; i++) { | ||
| status.append("-"); | ||
| } | ||
| return status.toString(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| public static void main(String[] args){ | ||
| try{ | ||
| Scanner sc = new Scanner(System.in); | ||
| //이름 입력받기 | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| String[] carnames = sc.nextLine().split(","); | ||
| List<Car> cars = new ArrayList<>(); | ||
| for (String name : carnames){ | ||
| cars.add(new Car(name)); | ||
| } | ||
| //회수 입력받기 | ||
| System.out.println("시도할 회수는 몇회인가요?"); | ||
| int racecount = sc.nextInt(); | ||
| if (racecount<=0){ | ||
| throw new IllegalArgumentException ("시도 횟수는 양수여야 합니다."); | ||
| } | ||
| //경주 | ||
| System.out.println("\n실행 결과"); | ||
| for (int i=0; i<racecount; i++){ | ||
| raceround(cars); | ||
| printroundresult(cars); | ||
| } | ||
|
|
||
| printwinners(cars); | ||
| } | ||
| catch(IllegalArgumentException e){ | ||
| System.out.println("[ERROR] " + e.getMessage()); | ||
| } | ||
| } | ||
| private static void raceround(List<Car> cars){ | ||
| for (Car car : cars){ | ||
| car.move(); | ||
| } | ||
| } | ||
|
|
||
| private static void printroundresult(List<Car> cars){ | ||
| for (Car car : cars) { | ||
| System.out.println(car.getstatus()); | ||
| } | ||
| System.out.println(); | ||
| } | ||
|
|
||
| private static void printwinners(List<Car> cars) { | ||
| int maxposition = 0; | ||
| for (Car car : cars) { | ||
| if (car.getPosition() > maxposition) { | ||
| maxposition = car.getPosition(); | ||
| } | ||
| } | ||
| List<String> winnerNames = new ArrayList<>(); | ||
| for (Car car : cars) { | ||
| if (car.getPosition() == maxposition) { | ||
| winnerNames.add(car.getname()); | ||
| } | ||
| } | ||
| //우승자 출력 | ||
| System.out.println("최종 우승자 : " + String.join(", ", winnerNames)); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
네이밍 규칙에 대해 알아보시고 적용해주세요. (PascalCase, camelCase)