Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
59 lines (47 loc) · 1.77 KB
/
Copy pathMain.java
File metadata and controls
59 lines (47 loc) · 1.77 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
import java.util.Scanner;
public class Main {
private static Scanner scanner;
public static void main(String[] args) {
scanner = new Scanner(System.in);
Car[] cars = makeRaceStartList();
Race race = new Race(cars);
Car winner = race.getLeader();
if (winner != null) {
System.out.printf("Самая быстрая машина: %s%n", winner.getName());
} else {
System.out.println("Победитель гонки не определен.");
}
}
public static Car[] makeRaceStartList() {
int CARS_TOTAL = 3;
Car[] raceMembers = new Car[CARS_TOTAL];
for (int i = 1; i <= CARS_TOTAL; i++) {
String carName = getCarName(i);
int speed = getCarSpeed(i);
Car car = new Car(carName, speed);
raceMembers[i - 1] = car;
}
return raceMembers;
}
public static String getCarName(int index) {
String name = "";
while (name.isEmpty()) {
System.out.printf("Введите название машины №%d:", index);
name = scanner.nextLine().trim();
}
return name;
}
public static int getCarSpeed(int index) {
int speed = 0;
while (speed <= 0 || speed > 250) {
System.out.printf("Введите скорость машины №%d (от 1 до 250 км/ч):", index);
try {
speed = scanner.nextInt();
} catch (Exception e) {
System.out.println("Допускаются только целые числа в диапазоне от 1 до 250. Попробуйте еще раз!");
scanner.nextLine();
}
}
return speed;
}
}