Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
71 lines (67 loc) · 3.03 KB
/
Copy pathMain.java
File metadata and controls
71 lines (67 loc) · 3.03 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
import static java.lang.Integer.parseInt;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Добро пожаловать на 24 часа Ле-Мана!");
Race race;
ArrayList<Car> racersList = new ArrayList<>();
String needAnotherRaceStr;
boolean bNeedAnotherRace;
while (true) {
racersList.clear();
while (true) {
System.out.println("Введите количество учавствующих в заезде автомобилей:");
String racersNum = scanner.next();
if (racersNum.matches("-?\\d+")) {
if (parseInt(racersNum) > 0) {
for (int i = 0; i < parseInt(racersNum); i++) {
racersList.add(appendCar(i));
}
break;
} else {
System.out.println("Некорректное число участников заезда");
}
} else System.out.println("Некорректное число участников заезда");
}
race = new Race(racersList);
race.startRace();
race.getResult();
while (true) {
System.out.println("Желаете провести еще один заезд? Да/Нет");
needAnotherRaceStr = scanner.next();
if (needAnotherRaceStr.equals("Да")) {
bNeedAnotherRace = true;
break;
} else if (needAnotherRaceStr.equals("Нет")) {
bNeedAnotherRace = false;
break;
} else System.out.println("Некорректный ввод");
}
if (!bNeedAnotherRace)
break;
}
}
public static Car appendCar(int number) {
String carName;
String carSpeed;
while (true) {
System.out.println("Введите название автомобиля №" + (number + 1));
carName = scanner.next();
if (carName.isBlank()) {
System.out.println("Некорректное название автомобиля");
} else break;
}
while (true) {
System.out.println("Введите скорость автомобиля №" + (number + 1) + " в диапазоне от 0 до 250 км\\ч");
carSpeed = scanner.next();
if (carSpeed.matches("-?\\d+")) {
if (parseInt(carSpeed) >= 0 && parseInt(carSpeed) <= 250) {
break;
} else System.out.println("Некорректная скорость");
} else System.out.println("Некорректная скорость");
}
return new Car(parseInt(carSpeed), carName);
}
}