Skip to content

Conversation

@JohnPrigozhin
Copy link
Owner

если при вводе имени машины или скорости сделать перенос строки (enter), то сообщение ошибки будет выведено столько же раз, сколько была перенесена строка, не нашёл красивого решения :(

Comment on lines +2 to +3
String name;
int speed;

Choose a reason for hiding this comment

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

Поля лучше пометить final, тем самым исключив возможность их модификации извне

Comment on lines +12 to +16
System.out.println("Введите название машины №" + i + ":");
name = scanner.nextLine();
if (name.trim().isEmpty()) {
System.out.println("Название не может быть пустым!");
}

Choose a reason for hiding this comment

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

Код для считывания непустой строки с ввода лучше вынести в отдельную функцию - код, разделённый на небольшие функции, легче читать, поддерживать и переиспользовать

speed = scanner.nextInt();
scanner.nextLine();

if (speed > 0 && speed <= 250) {

Choose a reason for hiding this comment

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

Минимальную и максимальную скорости лучше вынести в константы для повышения читабельности кода

Comment on lines +2 to +4
String leaderName = "";
int leaderDistance = 0;

Choose a reason for hiding this comment

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

Обе переменные лучше сделать приватными, т.к. они относятся к внутренней логике работы класса, а для получения машины-победителя у тебя уже есть функция getWinner()

scanner.nextLine(); }
}

Car car = new Car(name.trim(), speed);

Choose a reason for hiding this comment

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

trim() лучше сразу сделать при чтении строки, что по идее уже выполняется, но делается только для проверки в конструкции if - можно вызов trim() сделать один раз вместо двух в данном случае

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants