-
Notifications
You must be signed in to change notification settings - Fork 0
race #6
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?
race #6
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,64 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| Scanner scanner = new Scanner(System.in); | ||
| int n = 3; | ||
|
|
||
|
|
||
| Car[] cars = new Car[n]; | ||
|
|
||
| for (int i = 0; i < n; i++) { | ||
| System.out.println("Введите название машины №" + (i + 1) + ":"); | ||
| String name = scanner.nextLine(); | ||
|
|
||
| double velocity; | ||
| while (true) { | ||
|
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. Код для считывания скорости с ввода лучше вынести в отдельную функцию - код, разделённый на небольшие функции, легче читать, поддерживать и переиспользовать |
||
| System.out.println("— Введите скорость машины №" + (i + 1) + ":"); | ||
| velocity = scanner.nextDouble(); | ||
| scanner.nextLine(); | ||
|
|
||
| if (velocity >= 0 && velocity <= 250) break; | ||
|
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. Минимальную и максимальную скорости лучше вынести в константы для повышения читабельности кода |
||
| System.out.println("Неправильная скорость"); | ||
| } | ||
|
|
||
| cars[i] = new Car(name, velocity); | ||
| } | ||
|
|
||
| Car winner = Race.winner(cars); | ||
|
|
||
| if (winner == null) { | ||
| System.out.println("Нет машин с допустимой скоростью. Допустимая скорость от 0 до 250"); | ||
| } else { | ||
| System.out.println("Самый быстрый автомобиль: " + winner.name); | ||
|
|
||
| } | ||
|
|
||
| scanner.close(); | ||
| } | ||
| } | ||
|
|
||
| class Car { | ||
|
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. Код классов лучше выносить в отдельные файлы, чтобы не хранить весь код в одном файле |
||
| String name; | ||
| double velocity; | ||
|
Comment on lines
+42
to
+43
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. Поля лучше пометить |
||
|
|
||
| Car(String name, double velocity) { | ||
| this.name = name; | ||
| this.velocity = velocity; | ||
| } | ||
| } | ||
|
|
||
| class Race { | ||
| static Car winner(Car[] cars) { | ||
| Car winner = null; | ||
|
|
||
| for (Car c : cars) { | ||
| if (c.velocity >= 0 && c.velocity <= 250) { | ||
| if (winner == null || c.velocity * 24 > winner.velocity * 24) { | ||
| winner = c; | ||
| } | ||
| } | ||
| } | ||
| return winner; | ||
| } | ||
| } | ||
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.
От этой переменной можно избавиться, если вынести значение в константу -
private final static int CARS_AMOUNT = 3;