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
75 changes: 73 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,77 @@

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();
System.out.println("Добро пожаловать на гонку '24 часа Ле-Мана'!");

for (int i = 1; i <= 3; i++) {
System.out.println("\n Автомобиль №" + i + ".");

String name;
while (true) {
System.out.print("Введите название машины №" + i + ": ");
name = scanner.nextLine().trim();

if (!name.isEmpty()) {
break;
}
System.out.println("Название машины не может быть пустым. Попробуйте снова.");
}
int speed;
while (true) {
System.out.print("Введите скорость машины №" + i + " (0-250 км/ч): ");
try {
speed = Integer.parseInt(scanner.nextLine());

if (speed > 0 && speed <= 250) {
break;
} else {
System.out.println("Неправильная скорость! Скорость должна быть от 1 до 250 км/ч.");
}
} catch (NumberFormatException e) {
System.out.println("Ошибка! Введите целое число для скорости.");
}
}

Car car = new Car(name, speed);
race.updateLeader(car);
}

System.out.println("\nРЕЗУЛЬТАТЫ ГОНКИ");
System.out.println("Самая быстрая машина: " + race.name);
scanner.close();
}
static 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 calculateDistance() {
return 24 * speed;
}
}
static class Race {
private String name;
private int distance;
public Race() {
this.name = "";
this.distance = 0;
}
public void updateLeader(Car car) {
int carDistance = car.calculateDistance();
if (carDistance > distance) {
name = car.getName();
distance = carDistance;
}
}
}
}