Repository navigation
Expand file tree
/
Copy pathRacing.java
More file actions
59 lines (52 loc) · 2.24 KB
/
Copy pathRacing.java
File metadata and controls
59 lines (52 loc) · 2.24 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.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Racing {
ArrayList<Car> cars = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
public void start() {
for (int i = 0; i < 3; i++) {
String name = "";
while (true) {
System.out.println("Введите название машины №" + (i + 1));
name = scanner.nextLine();
if (!name.trim().isEmpty()) {
break;
} else {
System.out.println("Название автомобиля не может быть короче 1 символа");
}
}
int speed;
while (true) {
System.out.println("Введите скорость машины № " + (i + 1));
try {
speed = scanner.nextInt();
if (speed > 0 && speed <= 250) {
break;
} else {
System.out.println("Некорректная скорость. Попробуйте еще раз. Диапозон скорости от 0 до 250 км/ч");
}
} catch (InputMismatchException e) {
System.out.println("Некорректная скорость. Попробуйте еще раз. Введите число");
scanner.nextLine();
}
}
scanner.nextLine();
cars.add(new Car(name, speed));
}
if (cars.get(0).speed == cars.get(1).speed && cars.get(1).speed == cars.get(2).speed) {
System.out.println("Никто не выйграл, все машины оказались равны");
} else {
int fullDistance = 0;
String fastestCar = "";
for (Car car : cars) {
int distance = car.speed * 24;
if (distance > fullDistance) {
fullDistance = distance;
fastestCar = car.name;
}
}
System.out.println("Самая быстрая машина: " + fastestCar);
}
}
}