Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
81 lines (63 loc) · 3.48 KB
/
Copy pathMain.java
File metadata and controls
81 lines (63 loc) · 3.48 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
72
73
74
75
76
77
78
79
80
81
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Добро пожаловать на гонку \"«24 часа Ле-Мана»");
ArrayList<Racer> racersList = new ArrayList<>();
for (int i = 1; i <= 3; i++){
System.out.print("Введи имя гонщика №" + i + ": ");
String userInputName = scanner.nextLine();
while (checkName(userInputName)){
System.out.print("Ошибка! Имя не может быть пустым: ");
userInputName = scanner.nextLine();
}
// не могла никак придумать, как добавить проверку на пустую скорость
// нейронка предложила через цикл while с try-catch,
// получилось длиновато, поэтому решила всю проверку скорости вынести в отдельный метод checkSpeed
int userInputSpeed = checkSpeed(scanner, i);
racersList.add(new Racer(userInputName, userInputSpeed));
System.out.println("Гонщик №" + i + " создан: " + userInputName + ", скорость(км/ч): " + userInputSpeed);
}
System.out.print("\nСписок гонщиков: ");
for (Racer racer : racersList) {
System.out.print(racer.name + " ");
}
Winner winner = new Winner(racersList);
System.out.println("\n\nСпустя 24 часа...\n");
System.out.println("И победителем становится: гонщик " + winner.getRacer().name + " !!!");
System.out.println("За 24 часа он преодолел: " + winner.getDistance());
}
public static boolean checkName(String name){
return name.isEmpty();
}
public static int checkSpeed(Scanner scanner, int number){
while (true) {
System.out.print("Введи скорость гонщика №" + number + " (км/ч): ");
String input = scanner.nextLine(); //пришлось делать строковый ввод, чтобы проверить на пустую строку
// Проверяем, что строка не пустая
if (input.isEmpty()) {
System.out.println("Ошибка! Скорость не может быть пустой.");
continue;
}
try {
int userInputSpeed = Integer.parseInt(input); // Превращаем строку в число
if (userInputSpeed > 0 && userInputSpeed <= 250) {
return userInputSpeed;
}
System.out.println("Скорость должна быть > 0 и <= 250");
} catch (NumberFormatException e) {
System.out.print("Ошибка! Скорость должна быть числом:");
}
}
}
// public static Racer getWinner(ArrayList<Racer> racers){
// Racer winner = racers.getFirst(); // сначала запишем победителем первого гонщика
// for (Racer racer : racers) {
// if (racer.speed * 24 > winner.speed * 24) {
// winner = racer;
// }
// }
// return winner;
// }
}