-
Notifications
You must be signed in to change notification settings - Fork 981
Expand file tree
/
Copy pathMain.java
More file actions
89 lines (63 loc) · 2.99 KB
/
Main.java
File metadata and controls
89 lines (63 loc) · 2.99 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
79
80
81
82
83
84
85
86
87
88
89
import java.util.Scanner;
public class Main {
private static final int MIN_SPEED = 0;
private static final int MAX_SPEED = 250;
private static final int NUMBER_OF_CARS = 3;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Race race = new Race();
System.out.println("Добро пожаловать на гонку \"24 часа Ле-Мана\"!");
System.out.println();
for (int i = 1; i <= NUMBER_OF_CARS; i++) {
System.out.println();
System.out.println("--- Автомобиль №" + i + " ---");
String carName = getValidCarName(scanner, i);
int carSpeed = getValidCarSpeed(scanner, i);
Automobile car = new Automobile(carName, carSpeed);
race.determineLeader(car);
System.out.println("Автомобиль \"" + carName + "\" добавлен в гонку!");
}
System.out.println();
System.out.println("САМАЯ БЫСТРАЯ МАШИНА: " + race.getLeaderName());
scanner.close();
}
private static String getValidCarName(Scanner scanner, int carNumber) {
String carName = "";
boolean isValid = false;
while (!isValid) {
System.out.print("Введите название машины №" + carNumber + ": ");
carName = scanner.nextLine().trim();
// Проверка, что название не пустое
if (carName.isEmpty()) {
System.out.println("Ошибка: Название автомобиля не может быть пустым!");
} else {
isValid = true;
}
}
return carName;
}
private static int getValidCarSpeed(Scanner scanner, int carNumber) {
int speed = 0;
boolean isValid = false;
while (!isValid) {
System.out.print("Введите скорость машины №" + carNumber + " (1-250 км/ч): ");
// Проверяем, что введено целое число
if (scanner.hasNextInt()) {
speed = scanner.nextInt();
scanner.nextLine(); // Очистка буфера
// Проверка диапазона скорости
if (speed > MIN_SPEED && speed <= MAX_SPEED) {
isValid = true;
} else {
System.out.println("Неправильная скорость! Скорость должна быть от " +
(MIN_SPEED + 1) + " до " + MAX_SPEED + " км/ч.");
}
} else {
// Если введено не число
String invalidInput = scanner.nextLine();
System.out.println("Ошибка: \"" + invalidInput + "\" - это не число! Введите целое число.");
}
}
return speed;
}
}