Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
53 lines (47 loc) · 2.34 KB
/
Copy pathMain.java
File metadata and controls
53 lines (47 loc) · 2.34 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
import java.util.ArrayList;
import java.util.Scanner;
public class Main { //Main – класс, в котором происходит считывание пользовательского ввода и коммуникация с остальными классами.
public static void main(String[] args) {
ArrayList <Car> participants = new ArrayList<>(3);
Scanner scanner = new Scanner(System.in);
Race race = new Race();
System.out.println("— Привет!");
System.out.println("— Добро пожаловать на «24 часа Ле-Мана»");
for(int i = 1; i <= 3; i++) {
System.out.println("— Введите название машины №" + i + ": ");
String name = "";
while (true) {
name = scanner.nextLine();
if (name.trim().isEmpty()) {
System.out.println("Не ввел название машины.");
} else {
System.out.println("Отлично"); //
break;
}
}
System.out.println("— Введите скорость машины " + name + ": ");
while (true) {
String input = scanner.nextLine().trim();
if (input.isEmpty()) {
System.out.println("— Ввод не должен быть пустым. Введи скорость от 1 до 250");
continue;
}
try {
int speed = Integer.parseInt(input);
if (speed <= 0 || speed > 250) {
System.out.println("— Неправильная скорость. Введи скорость от 1 до 250");
continue;
} else {
participants.add(new Car(name, speed));
break;
}
} catch (NumberFormatException e) {
System.out.println("— Неверные данные. Введи действительный номер скорости");
}
}
}
race.calculation(participants);
System.out.println("— Самая быстрая машина: " + race.getWinner());
scanner.close();
}
}