Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
77 lines (63 loc) · 2.23 KB
/
Copy pathMain.java
File metadata and controls
77 lines (63 loc) · 2.23 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
// public class Main {
//public static void main(String[] args) {
// System.out.println("Hello world!");
// }
//}
import java.util.Scanner;
class Car {
String name;
int speed;
public Car(String name, int speed) {
this.name = name;
this.speed = speed;
}
}
class Race {
String leaderName = "";
int leaderDistance = 0;
public void determineLeader(Car car) {
int currentCarDistance = 24 * car.speed;
if (currentCarDistance > leaderDistance) {
leaderDistance = currentCarDistance;
leaderName = car.name;
}
}
}
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.println("Введите название автомобиля №" + i + ":");
name = scanner.nextLine();
if (!name.trim().isEmpty()) {
break;
}
System.out.println("Ошибка! Название автомобиля не может быть пустым.");
}
int speed;
while (true) {
System.out.println("Введите скорость автомобиля №" + i + " (0-250):");
if (scanner.hasNextInt()) {
speed = scanner.nextInt();
scanner.nextLine();
if (speed >= 0 && speed <= 250) {
break;
} else {
System.out.println("Ошибка! Скорость должна быть от 0 до 250.");
}
} else {
System.out.println("Ошибка! Попробуйте снова! Введите целое число.");
scanner.next();
scanner.nextLine();
}
}
Car car = new Car(name, speed);
race.determineLeader(car);
}
System.out.println("Самый быстрый автомобиль: " + race.leaderName);
scanner.close();
}
}