forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Изменения по проектной работе №1 #1
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
Siustoster
wants to merge
2
commits into
main
Choose a base branch
from
dev
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| public class Car { | ||
| private int speed; | ||
| private String name; | ||
| private int distance; | ||
|
|
||
| Car(int speed, String name) { | ||
| this.speed = speed; | ||
| this.name = name; | ||
| } | ||
|
|
||
| public int getSpeed() { | ||
| return speed; | ||
| } | ||
|
|
||
| public void setSpeed(int speed) { | ||
| this.speed = speed; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public int getDistance() { | ||
| return distance; | ||
| } | ||
|
|
||
| public void setDistance(int distance) { | ||
| this.distance = distance; | ||
| } | ||
| } |
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,6 +1,71 @@ | ||
| import static java.lang.Integer.parseInt; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static Scanner scanner = new Scanner(System.in); | ||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| System.out.println("Добро пожаловать на 24 часа Ле-Мана!"); | ||
| Race race; | ||
| ArrayList<Car> racersList = new ArrayList<>(); | ||
| String needAnotherRaceStr; | ||
| boolean bNeedAnotherRace; | ||
| while (true) { | ||
| racersList.clear(); | ||
| while (true) { | ||
| System.out.println("Введите количество учавствующих в заезде автомобилей:"); | ||
| String racersNum = scanner.next(); | ||
| if (racersNum.matches("-?\\d+")) { | ||
| if (parseInt(racersNum) > 0) { | ||
| for (int i = 0; i < parseInt(racersNum); i++) { | ||
| racersList.add(appendCar(i)); | ||
| } | ||
| break; | ||
| } else { | ||
| System.out.println("Некорректное число участников заезда"); | ||
| } | ||
| } else System.out.println("Некорректное число участников заезда"); | ||
| } | ||
| race = new Race(racersList); | ||
| race.startRace(); | ||
| race.getResult(); | ||
| while (true) { | ||
| System.out.println("Желаете провести еще один заезд? Да/Нет"); | ||
| needAnotherRaceStr = scanner.next(); | ||
| if (needAnotherRaceStr.equals("Да")) { | ||
| bNeedAnotherRace = true; | ||
| break; | ||
| } else if (needAnotherRaceStr.equals("Нет")) { | ||
| bNeedAnotherRace = false; | ||
| break; | ||
| } else System.out.println("Некорректный ввод"); | ||
| } | ||
| if (!bNeedAnotherRace) | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| public static Car appendCar(int number) { | ||
| String carName; | ||
| String carSpeed; | ||
| while (true) { | ||
| System.out.println("Введите название автомобиля №" + (number + 1)); | ||
| carName = scanner.next(); | ||
| if (carName.isBlank()) { | ||
| System.out.println("Некорректное название автомобиля"); | ||
| } else break; | ||
| } | ||
| while (true) { | ||
| System.out.println("Введите скорость автомобиля №" + (number + 1) + " в диапазоне от 0 до 250 км\\ч"); | ||
| carSpeed = scanner.next(); | ||
| if (carSpeed.matches("-?\\d+")) { | ||
| if (parseInt(carSpeed) >= 0 && parseInt(carSpeed) <= 250) { | ||
| break; | ||
| } else System.out.println("Некорректная скорость"); | ||
| } else System.out.println("Некорректная скорость"); | ||
| } | ||
| return new Car(parseInt(carSpeed), carName); | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Comparator; | ||
|
|
||
| public class Race { | ||
| private ArrayList<Car> racers = new ArrayList<>(); | ||
|
|
||
| Race(ArrayList<Car> Racers) { | ||
| this.racers.addAll(Racers); | ||
| } | ||
|
|
||
| public void startRace() { | ||
| System.out.println("Начинаем заезд!"); | ||
| for (Car car : racers) { | ||
| car.setDistance(car.getSpeed() * 24); | ||
| } | ||
| } | ||
|
|
||
| public void getResult() { | ||
| racers.sort(Comparator.comparingInt(Car::getDistance)); | ||
| System.out.println("Победитель заезда: " + racers.getLast().getName() + ", проехавший " | ||
| + racers.getLast().getDistance() + " за 24 часа"); | ||
| } | ||
| } |
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.
Можно использовать nextLine() вместо next() для строк
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.
Попробовал использовать nextLine(), но почему-то выполняться программа стала некорректно - не ждет пока пользователь что-то введет, а сразу идет выполняться дальше и выдает из-за этого ошибку проверки. Вернул next()