-
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
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 |
|---|---|---|
| @@ -1,6 +1,55 @@ | ||
| 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 carName; | ||
| while (true) { | ||
| carName = scanner.nextLine().trim(); | ||
| if (carName.isEmpty()) { | ||
| System.out.println("Название автомобиля не может быть пустым или состоять из пробелов, повторите ввод"); | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| System.out.println("Введите скорость автомобиля №" + i); | ||
| int carSpeed; | ||
| while (true) { | ||
| if (scanner.hasNextInt()) { | ||
| carSpeed = scanner.nextInt(); | ||
| 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. Минимальную и максимальную скорости лучше вынести в константы для повышения читабельности кода |
||
| System.out.println("Скорость не может быть отрицательной или свыше 250, повторите ввод"); | ||
| } else { | ||
| scanner.nextLine(); | ||
| break; | ||
| } | ||
| } | ||
| else { | ||
| System.out.println("Некорректное значение скорости, повторите ввод"); | ||
| scanner.nextLine(); | ||
| } | ||
|
|
||
| } | ||
| new Vehicle(carName, carSpeed); | ||
| race.Race(carName, carSpeed); | ||
| } | ||
| scanner.close(); | ||
| System.out.println("Машина под названием " + race.leader + " является лидером гонки"); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
Comment on lines
+46
to
+55
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. Лишние пустые строчки лучше удалить, чтобы не засоряли код. Достаточно использовать 1 пустую строку для разделения логически делимых блоков в коде |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| public class Race { | ||
| String leader; | ||
|
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. Саму переменную лучше сделать приватной, чтобы исключить возможность изменения её значения извне, а для получения результата гонки написать отдельную функцию-геттер |
||
| private int distance = 0; | ||
|
|
||
| public String Race(String carName, int speed) { | ||
|
|
||
| int carDistance = 24 * speed; | ||
| if (carDistance > distance) { | ||
| distance = carDistance; | ||
| leader = carName; | ||
| } | ||
| return leader; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| public class Vehicle { | ||
| String carName; | ||
| int carSpeed; | ||
|
Comment on lines
+2
to
+3
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 Vehicle(String carName, int speed) { | ||
| this.carName = carName; | ||
| this.carSpeed = speed; | ||
| } | ||
| } | ||
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.
Код для считывания непустой строки с ввода лучше вынести в отдельную функцию - код, разделённый на небольшие функции, легче читать, поддерживать и переиспользовать