forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
67 lines (57 loc) · 2.8 KB
/
Main.java
File metadata and controls
67 lines (57 loc) · 2.8 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
import java.util.Scanner;
public class Main {
// Вынес что возможно в константы
public static final int minSpeed = 0;
public static final int maxSpeed = 250;
public static final int racingTime = 24;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// При регистрации участников сразу вычисляем победителя гонок
Car leader = null;
int maxDistance = 0;
for (int i = 0; i < 3; i++) {
int numberOfParticipant = i + 1;
System.out.println("Введите имя " + numberOfParticipant + " участника");
String currentName;
while ((currentName = scanner.nextLine()).trim().isEmpty()) {
System.out.println("Вы ввели некорректное имя участника!");
}
int currentSpeed = inputSpeed(scanner, numberOfParticipant);
scanner.nextLine();
Car currentCar = new Car(currentName, currentSpeed);
int currentDistance = currentCar.getDistance(racingTime);
if (leader == null) {
leader = currentCar;
maxDistance = currentDistance;
} else {
if (maxDistance < currentDistance) {
leader = currentCar;
maxDistance = currentDistance;
}
}
}
System.out.println("В гонке победил автомобиль " + leader.name);
scanner.close();
}
// Вынес код ввода скорости в отдельную функцию
public static int inputSpeed(Scanner scanner, int numberOfParticipant) {
int currentSpeed = -1;
System.out.println("Введите скорость автомобиля " + numberOfParticipant + " участника в диапазоне от " + minSpeed + " до " + maxSpeed + " км/ч");
while (true) {
if (!scanner.hasNextInt()) {
System.out.println("Вы ввели что-то не то!");
// что-то без этого уходит в бесконечный цикл, наверное нужно ввод как-то прочитать
scanner.nextLine();
} else {
currentSpeed = scanner.nextInt();
if (currentSpeed <= minSpeed || currentSpeed >= maxSpeed) {
System.out.println("Введите значение скорости в диапазоне от " + minSpeed + " до " + maxSpeed + " км/ч");
scanner.nextLine();
} else {
break;
}
}
}
return currentSpeed;
}
}