Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
45 lines (37 loc) · 1.58 KB
/
Copy pathMain.java
File metadata and controls
45 lines (37 loc) · 1.58 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
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++) {
System.out.println("Введите данные для автомобиля №" + i + ":");
String name = readCarName(scanner);
int speed = readValidSpeed(scanner);
Auto auto = new Auto(name, speed);
race.determineNewLeader(auto);
System.out.println("Победитель гонки: " + race.getLeaderName());
}
}
private static String readCarName(Scanner scanner) {
System.out.print("Введите название автомобиля: ");
return scanner.nextLine();
}
private static int readValidSpeed(Scanner scanner) {
int speed = 0;
boolean validInput = false;
while (!validInput) {
System.out.print("Введите скорость автомобиля (0-250 км/ч): ");
try {
speed = Integer.parseInt(scanner.nextLine());
if (speed >= 0 && speed <= 250) {
validInput = true;
} else {
System.out.println("Неверная скорость! Попробуйте снова.");
}
} catch (NumberFormatException e) {
System.out.println("Введённое значение не является числом. Попробуйте снова.");
}
}
return speed;
}
}