-
Notifications
You must be signed in to change notification settings - Fork 0
Попытка написать гонки номер раз #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
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,19 @@ | ||
| public class Car { | ||
| private String name; | ||
| private int speed; | ||
|
|
||
| public Car(String name, int speed) { | ||
| this.name = name; | ||
| this.speed = speed; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public int getSpeed() { | ||
| return speed; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,51 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| Race race = new Race(); | ||
|
|
||
| System.out.println("24 часа Ле-Мана"); | ||
|
|
||
| for (int i = 1; i <= 3; i++) { | ||
| System.out.println("\nАвтомобиль №" + i); | ||
| System.out.println("Введите название машины: "); | ||
| String carName = scanner.nextLine(); | ||
|
|
||
| while (carName.trim().isEmpty()) { | ||
| System.out.println("Ошибка: название не может быть пустым!"); | ||
| System.out.println("Введите название машины:"); | ||
| carName = scanner.nextLine(); | ||
| } | ||
|
Comment on lines
+15
to
+19
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. Код для считывания непустой строки с ввода лучше вынести в отдельную функцию - код, разделённый на небольшие функции, легче читать, поддерживать и переиспользовать |
||
|
|
||
| int carSpeed = 0; | ||
| boolean speedIsCorrect = false; | ||
|
|
||
| while (!speedIsCorrect) { | ||
| System.out.println("Введите скорость машины: "); | ||
|
|
||
| if (scanner.hasNextInt()) { | ||
| carSpeed = scanner.nextInt(); | ||
| scanner.nextLine(); | ||
|
|
||
| if (carSpeed > 0 && carSpeed <= 250) { | ||
|
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. Минимальную и максимальную скорости лучше вынести в константы для повышения читабельности кода |
||
| speedIsCorrect = true; | ||
| } else if (carSpeed <= 0) { | ||
| System.out.println("Ошибка: скорость должна быть больше 0!"); | ||
| } else { | ||
| System.out.println("Неправильная скорость!"); | ||
| } | ||
| } else { | ||
| System.out.println("Ошибка: введите целое число!"); | ||
| scanner.nextLine(); | ||
| } | ||
| } | ||
|
|
||
| Car car = new Car(carName, carSpeed); | ||
| race.updateLeader(car); | ||
| } | ||
|
|
||
| scanner.close(); | ||
| System.out.println("\n" + race.getWinner()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| public class Race { | ||
| private String leaderName; | ||
| private int leaderDistance; | ||
|
|
||
| public Race() { | ||
| this.leaderName = ""; | ||
| this.leaderDistance = 0; | ||
| } | ||
| public void updateLeader(Car car) { | ||
| int carDistance = car.getSpeed() * 24; | ||
|
|
||
| if (carDistance > leaderDistance) { | ||
| leaderName = car.getName(); | ||
| leaderDistance = carDistance; | ||
| } | ||
| } | ||
|
|
||
| public String getWinner() { | ||
| return "Самая быстрая машина: " + leaderName; | ||
| } | ||
| } |
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.
Поля лучше пометить
final, тем самым исключив возможность их модификации извне. Тогда геттеры можно будет удалить и безопасно получать доступ напрямую по названию поля