-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathMain.java
More file actions
78 lines (64 loc) · 2.22 KB
/
Copy pathMain.java
File metadata and controls
78 lines (64 loc) · 2.22 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
78
package org.example;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Race race = new Race();
for (int i = 1; i <= 3; i++) {
String name = "";
while (true) {
System.out.print("— Введите название машины №" + i + ": ");
name = scanner.nextLine();
if (!name.trim().isEmpty()) {
break;
} else {
System.out.println("— Ошибка: Название автомобиля не может быть пустым");
}
}
int speed = 0;
while (true) {
System.out.print("— Введите скорость машины №" + i + ": ");
String speedInput = scanner.nextLine();
if (speedInput.trim().isEmpty()) {
System.out.println("— Неправильная скорость");
continue;
}
try {
speed = Integer.parseInt(speedInput);
if (speed > 0 && speed <= 250) {
break;
} else {
System.out.println("— Неправильная скорость");
}
} catch (NumberFormatException e) {
System.out.println("— Неправильная скорость");
}
}
Car car = new Car(name, speed);
race.checkNewParticipant(car);
}
race.printLeader();
}
}
class Car {
String name;
int speed;
public Car(String name, int speed) {
this.name = name;
this.speed = speed;
}
}
class Race {
String leaderName = "";
int maxDistance = 0;
public void checkNewParticipant(Car car) {
int distance = car.speed * 24;
if (distance > maxDistance) {
maxDistance = distance;
leaderName = car.name;
}
}
public void printLeader() {
System.out.println("Самая быстрая машина: " + leaderName);
}
}