forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
64 lines (49 loc) · 2.09 KB
/
Main.java
File metadata and controls
64 lines (49 loc) · 2.09 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Car[] cars = new Car[3];
System.out.println("=== 24 часа Ле-Мана ===");
// Ввод данных для трех автомобилей
for (int i = 0; i < 3; i++) {
System.out.println("— Введите название машины №" + (i + 1) + ":");
String name = scanner.nextLine();
int speed = getValidSpeed(scanner, i + 1);
cars[i] = new Car(name, speed);
}
// Определяем победителя
Race race = new Race(cars);
Car winner = race.getLeader();
// Выводим результат
System.out.println("Самая быстрая машина: " + winner.getName());
scanner.close();
}
/**
* Метод для получения корректной скорости с проверкой
*/
private static int getValidSpeed(Scanner scanner, int carNumber) {
int speed = 0;
boolean isValid = false;
while (!isValid) {
System.out.println("— Введите скорость машины №" + carNumber + ":");
String input = scanner.nextLine();
try {
// Проверяем на дробное число
if (input.contains(".") || input.contains(",")) {
System.out.println("— Неправильная скорость");
continue;
}
speed = Integer.parseInt(input);
// Проверяем диапазон
if (speed <= 0 || speed > 250) {
System.out.println("— Неправильная скорость");
} else {
isValid = true;
}
} catch (NumberFormatException e) {
System.out.println("— Неправильная скорость");
}
}
return speed;
}
}