-
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 Auto { | ||
| String name; | ||
| int speed; | ||
|
|
||
| public Auto(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,44 @@ | ||
| 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(); | ||
|
|
||
| for (int i = 1; i <= 3; i++) { | ||
| System.out.println("Автомобиль #" + i); | ||
|
|
||
| String name = getName(scanner); | ||
| int speed = getValidSpeed(scanner, name); | ||
|
|
||
| race.addCar(new Auto(name, speed)); | ||
| System.out.println("Текущий лидер: " + race.getCurrentLeader()); | ||
| } | ||
|
|
||
| System.out.println("\nФинальный победитель: " + race.getCurrentLeader()); | ||
| } | ||
|
|
||
| private static String getName(Scanner scanner) { | ||
| System.out.print("Название: "); | ||
| return scanner.nextLine(); | ||
| } | ||
|
|
||
| private static int getValidSpeed(Scanner scanner, String name) { | ||
| while (true) { | ||
| System.out.print("Скорость для " + name + " (1-250): "); | ||
|
|
||
| if (scanner.hasNextInt()) { | ||
| int speed = scanner.nextInt(); | ||
| scanner.nextLine(); // Очистка буфера | ||
|
|
||
| if (speed > 0 && speed <= 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. Минимальную и максимальную скорости лучше вынести в константы с говорящими названиями для повышения читабельности кода |
||
| return speed; | ||
| } | ||
| System.out.println("Ошибка: скорость должна быть 1-250!"); | ||
| } else { | ||
| System.out.println("Ошибка: введите целое число!"); | ||
| scanner.nextLine(); // Очистка неверного ввода | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public class Race { | ||
| String currentLeaderName = "Лидер отсутствует"; | ||
| int currentLeaderDistance = 0; | ||
|
Comment on lines
+4
to
+5
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. Данные поля можно сделать приватными (пометив их модификатором |
||
| ArrayList<Auto> cars = new ArrayList<>(); | ||
|
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. От хранения массива машин и лишнего цикла при определении победителя можно избавиться, если при вводе данных сразу вычислять победителя и хранить его в отдельной переменной, тогда программа будет требовать меньше памяти и работать быстрее |
||
|
|
||
| public void addCar(Auto car) { | ||
| cars.add(car); | ||
| updateLeader(car); | ||
| } | ||
|
|
||
| private void updateLeader(Auto newCar) { | ||
| int newDistance = 24 * newCar.getSpeed(); | ||
|
|
||
| if (newDistance > currentLeaderDistance) { | ||
| currentLeaderDistance = newDistance; | ||
| currentLeaderName = newCar.getName(); | ||
| } | ||
| } | ||
|
|
||
| public String getCurrentLeader() { | ||
| return currentLeaderName; | ||
| } | ||
| } | ||
|
|
||
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, тем самым исключив возможность их модификации извне. Таким образом, геттеры можно будет удалить и получать значения полей по прямому доступу к ним