-
Notifications
You must be signed in to change notification settings - Fork 975
Expand file tree
/
Copy pathMain.java
More file actions
77 lines (71 loc) · 2.63 KB
/
Main.java
File metadata and controls
77 lines (71 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
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;
}
}
}
}