Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 70 additions & 2 deletions src/main/java/Main.java
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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Более подходящим неймингом будет простое name

private int speed;
Comment on lines +4 to +5

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Поля лучше пометить final, тем самым исключив возможность их модификации извне. Тогда можно будет удалить геттеры и сделать доступ к переменным напрямую

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;

Choose a reason for hiding this comment

The 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) {

Choose a reason for hiding this comment

The 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());

}
}