-
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: dev
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 +1,2 @@ | ||
| # Пустой репозиторий для работы с Java кодом в Android Studio | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| public class Car { | ||
| String model; | ||
| int speed; | ||
|
|
||
| public Car(String model, int speed) { | ||
| this.model = model; | ||
| this.speed = speed; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,40 @@ | ||
| import java.util.Scanner; // Импортируем сканер для ввода с клавиатуры (В задании было написано, что для ввода данных поможет класс Scanner. Я посмотрел в задании, что нужны методы nextInt() для цифр и next() для слов, и подключил его, чтобы программа могла считывать мои ответы, просто я не помню изучали мы его или нет) | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| System.out.println("Добро пожаловать на гонку '24 часа Ле-Мана'!"); | ||
|
|
||
| Race race = new Race(); | ||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| for (int i = 1; i <= 3; i++) { | ||
| System.out.println("Введите название машины №" + i + ":"); | ||
| String carModel = scanner.next(); | ||
|
|
||
| int carSpeed = 0; | ||
|
|
||
| while (true) { | ||
| System.out.println("Введите скорость машины №" + i + " (от 0 до 250):"); | ||
| if (scanner.hasNextInt()) { | ||
|
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. Код для считывания скорости с консоли ввода лучше вынести в отдельную функцию - код, разделённый на небольшие функции, легче читать, поддерживать и переиспользовать |
||
| 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. Минимальную и максимальную скорости лучше вынести в константы для повышения читабельности кода |
||
| break; | ||
| } else { | ||
| System.out.println("Ошибка! Скорость должна быть больше 0 и не более 250."); | ||
| } | ||
| } else { | ||
| System.out.println("Ошибка! Нужно ввести целое число."); | ||
| scanner.next(); | ||
| } | ||
| } | ||
|
|
||
| Car newCar = new Car(carModel, carSpeed); | ||
|
|
||
| race.determineWinner(newCar); | ||
| } | ||
|
|
||
| System.out.println("--- Гонка завершена! ---"); | ||
| System.out.println("Самая быстрая машина: " + race.leaderName); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| public class Race { | ||
| String leaderName = ""; | ||
| int leaderDistance = 0; | ||
|
|
||
| public void determineWinner(Car car) { | ||
| int distance = car.speed * 24; | ||
|
|
||
| if (distance > leaderDistance) { | ||
| leaderDistance = distance; | ||
| leaderName = car.model; | ||
| } | ||
| } | ||
| } |
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, тем самым исключив возможность их модификации извне