-
Notifications
You must be signed in to change notification settings - Fork 0
ПР 2 #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?
ПР 2 #1
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 @@ | ||
| # Пустой репозиторий для работы с Java кодом в Android Studio | ||
| 24 часа Ле-Мана |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| public class Car { | ||
| private String name; | ||
| private int speed; | ||
|
|
||
| public Car(String name, int speed) { | ||
| this.name = name; | ||
| this.speed = speed; | ||
| } | ||
| public String getName() { | ||
| return name; | ||
| } | ||
| public int getSpeed() { | ||
| return speed; | ||
| } | ||
| public int mileage() { | ||
| return 24 * speed; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,46 @@ | ||
|
|
||
| import java.util.Scanner; | ||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| public static void main(String[] args){ | ||
| System.out.println("24 часа Ле-Мана"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| Car[] cars = new Car[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. От хранения массива машин и лишнего цикла при определении победителя можно избавиться, если при вводе данных сразу вычислять победителя и хранить его в отдельной переменной, тогда программа будет требовать меньше памяти и работать быстрее |
||
| Race race = new Race(); | ||
|
|
||
| for (int i = 0; i < cars.length; i++) { | ||
| System.out.println("Автомобиль " + (i + 1) + ":"); | ||
|
|
||
| String name; | ||
| while (true) { | ||
| System.out.println("Введите название автомобиля: "); | ||
| name = scanner.nextLine().trim(); | ||
| if(!name.isEmpty()){ | ||
| break; | ||
| } | ||
| System.out.println("Ошибка: название автомобиля не может быть пустым!"); | ||
| } | ||
|
|
||
| int speed = 0; | ||
| while (true) { | ||
| System.out.println("Введите скорость автомобиля (1-250 км/ч): "); | ||
| if (scanner.hasNextInt()) { | ||
| speed = scanner.nextInt(); | ||
| scanner.nextLine(); | ||
| 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("Скорость должна быть от 1 до 250."); | ||
| } | ||
| } else { | ||
| System.out.println("Введите целое число"); | ||
| scanner.next(); | ||
| } | ||
| } | ||
|
|
||
| cars[i] = new Car(name, speed); | ||
| } | ||
| } | ||
|
|
||
| Car winner = race.raceWinner(cars); | ||
| System.out.println("Самая быстрая машина: " + winner.getName()); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| public class Race { | ||
| public Car raceWinner(Car[] cars) { | ||
| Car winner = cars[0]; | ||
| int maxDistance = cars[0].mileage(); | ||
| for (int i = 1; i < cars.length; i++) { | ||
| int currentDistance = cars[i].mileage(); | ||
| if (currentDistance > maxDistance) { | ||
| maxDistance = currentDistance; | ||
| winner = cars[i]; | ||
| } | ||
| } | ||
| 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.
Переменные можно пометить
finalи убрать геттеры с модификатором private - тогда можно будет иметь такой же надёжный и безопасный доступ к данным по имени переменной