Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
57 lines (49 loc) · 2.47 KB
/
Copy pathMain.java
File metadata and controls
57 lines (49 loc) · 2.47 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Приветствую вас на гонке «24 часа Ле-Мана»!");
Scanner scanner = new Scanner(System.in);
Race carLeader = new Race();
for (int i = 1; i <= 3; ++i) {
System.out.println("— Введите название машины №" + i + ":");
String inCarBrand = currectBrand(scanner);
System.out.println("— Введите скорость машины №" + i + ":");
int inCarSpeed = currectSpeed(scanner);
carLeader.makeRace(new Car(inCarBrand, inCarSpeed));
}
if (carLeader.draw == 0) {
System.out.println("Победитель гонки «24 часа Ле-Мана»: " + carLeader.leadRace);
System.out.println("Финишировавшая машина проехала: " + carLeader.leadDistance + " км");
} else {
System.out.println("Победитель гонки «24 часа Ле-Мана» не установлен, так как ничья.");
System.out.println("Максимальное расстояние пройденное за гонку: " + carLeader.leadDistance);
}
}
public static int currectSpeed(Scanner scanner1) {
while (true) {
if (scanner1.hasNextInt()) {
int carSpeed = scanner1.nextInt();
scanner1.nextLine();
if (carSpeed >= 0 && carSpeed <= 250) {
return carSpeed;
} else {
System.out.println("Неправильная скорость. Введите скорость в диапозоне от 0 до 250.");
}
} else {
System.out.println("Некорректный ввод. Введите скорость в целочисленном диапозоне от 0 до 250.");
scanner1.nextLine();
}
}
}
public static String currectBrand(Scanner scanner2) {
while (true) {
String carBrand = scanner2.nextLine().trim();
if (!carBrand.isEmpty()) {
return carBrand;
}
else {
System.out.println("Вы оставили поле пустым, попробуйте ввести название машины еще раз: ");
}
}
}
}