-
Notifications
You must be signed in to change notification settings - Fork 0
первый проект #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,74 @@ | ||
|
|
||
| public class Main { | ||
| import java.util.Scanner; | ||
| class Car { | ||
| private String make; | ||
| private int speed; | ||
|
Comment on lines
+4
to
+5
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 Car(String make, int speed) { | ||
| this.make = make; | ||
| this.speed = speed; | ||
| } | ||
| public double calculateDistance(){ | ||
| return speed * 24.0; | ||
|
|
||
| } | ||
| public String getMake(){ | ||
| return make; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } | ||
| class Race { | ||
| private Car[] participants; | ||
|
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 Race(Car[] participants) { | ||
| this.participants = participants; | ||
| } | ||
|
|
||
| public Car determineWinner() { | ||
| Car leader = participants[0]; | ||
| for (int i = 1; i < participants.length; i++) { | ||
| if (participants[i].calculateDistance() > leader.calculateDistance()) { | ||
| leader = participants[i]; | ||
| } | ||
| } | ||
| return leader; | ||
| } | ||
| } | ||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| Car[] cars = new Car[3]; | ||
|
|
||
| for (int i = 0; i < 3; i++) { | ||
| System.out.println("Введите данные для автомобиля " + (i + 1) + ":"); | ||
|
|
||
| System.out.print("Название: "); | ||
| String make = scanner.nextLine(); | ||
|
|
||
| int speed = 0; | ||
| boolean validSpeed = false; | ||
| while (!validSpeed) { | ||
| System.out.print("Скорость (1-250 км/ч): "); | ||
| try { | ||
| speed = Integer.parseInt(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. Минимальную и максимальную скорости лучше вынести в константы для повышения читабельности кода |
||
| validSpeed = true; | ||
| } else { | ||
| System.out.println("Ошибка: скорость должна быть от 1 до 250 км/ч!"); | ||
| } | ||
| } catch (NumberFormatException e) { | ||
| System.out.println("Ошибка: введите целое число!"); | ||
| } | ||
| } | ||
|
|
||
| cars[i] = new Car(make, speed); | ||
| System.out.println("---"); | ||
| } | ||
|
|
||
| Race race = new Race(cars); | ||
| Car winner = race.determineWinner(); | ||
| System.out.println("Победитель гонки '24 часа Ле-Мана': " + winner.getMake()); | ||
|
|
||
| } | ||
| } | ||
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.
Более подходящим неймингом будет простое
name