Repository navigation
Проектная работа №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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| public class Car { | ||
| String name; | ||
| double speed; | ||
|
|
||
| public Car(String name, double speed) { | ||
| this.name = name; | ||
| this.speed = speed; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public double 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 = 0; i < 3; i++) { | ||
| String name; | ||
| while (true) { | ||
| System.out.println("Введите название машины №" + (i + 1) + ":"); | ||
| name = scanner.nextLine().trim(); | ||
| if (!name.isEmpty()) { | ||
| break; | ||
| } else { | ||
| System.out.println("Ошибка! Название не может быть пустым"); | ||
| } | ||
| } | ||
|
Comment on lines
+10
to
+18
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. Код для считывания непустой строки с ввода лучше вынести в отдельную функцию - код, разделённый на небольшие функции, легче читать, поддерживать и переиспользовать |
||
|
|
||
| double speed; | ||
| 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) + ":"); | ||
| String input = scanner.nextLine().trim(); | ||
| try { | ||
| speed = Double.parseDouble(input); | ||
| 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. Минимальную и максимальную скорости лучше вынести в константы для повышения читабельности кода |
||
| break; | ||
| } else { | ||
| System.out.println("Ошибка! Скорость должна быть > 0 и <= 250"); | ||
| } | ||
| } catch (NumberFormatException e) { | ||
| System.out.println("Ошибка! Введите корректное значение скорости"); | ||
| } | ||
| } | ||
|
|
||
| Car car = new Car(name, speed); | ||
| race.updateWinner(car); | ||
| } | ||
|
|
||
| scanner.close(); | ||
|
|
||
| System.out.println("Победитель гонки: " + race.getWinnerName()); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| public class Race { | ||
| String winnerName = ""; | ||
| double winnerDistance = 0; | ||
|
|
||
| void updateWinner(Car car) { | ||
| double distance = car.getSpeed() * 24; | ||
|
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. 24 лучше вынести в константу |
||
| if (distance > winnerDistance) { | ||
| winnerDistance = distance; | ||
| winnerName = car.getName(); | ||
| } | ||
| } | ||
|
|
||
| public String getWinnerName() { | ||
| return winnerName; | ||
| } | ||
| } | ||
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, тем самым исключив возможность их модификации извне