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
43 lines (40 loc) · 1.67 KB
/
Main.java
File metadata and controls
43 lines (40 loc) · 1.67 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
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
{System.out.println("Гонка! 24 часа Ле-Мана");}
Scanner scanner= new Scanner(System.in);
Race race = new Race();
for (int i = 0; i < 3; i++) {
String carName = readCarName(scanner);
int carSpeed = readCarSpeed(scanner);
Car car;
car = new Car(carName, carSpeed);
race.updateLeader(car);
}
System.out.println("Самая быстрая машина : " + race.getCurrentLeader().getName());
scanner.close();
} private static String readCarName(Scanner scanner) {
System.out.println("Введите название автомобиля:");
return scanner.nextLine();
}
private static int readCarSpeed(Scanner scanner) {
int carSpeed;
while (true) {
System.out.println("Введите скорость автомобиля (от 0 до 250 км/ч):");
try {
carSpeed = scanner.nextInt();
if (carSpeed >= 0 && carSpeed <= 250) {
scanner.nextLine();
break;
} else {
System.out.println("Скорость должна быть (от 0 до 250 км/ч.):");
}
} catch (InputMismatchException e) {
System.out.println("Введите целое числовое значение.");
scanner.nextLine(); // Очистить буфер сканера
}
}
return carSpeed;
}
}