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
57 lines (49 loc) · 2.06 KB
/
Main.java
File metadata and controls
57 lines (49 loc) · 2.06 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.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ArrayList<Car> cars = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
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("Название машины не может быть пустым, попробуй снова.");
}
}
int speed;
while (true) {
System.out.println("Введите скорость машины №" + (i + 1) + ": ");
if (scanner.hasNextInt()) {
speed = scanner.nextInt();
scanner.nextLine();
if (speed > 0 && speed <= 250) {
break;
} else {
System.out.println("Некорректная скорость машины, попробуй ещё раз");
}
} else {
System.out.println("Введено не число, попробуй снова.");
scanner.next();
}
}
Car car = new Car(name, speed);
cars.add(car);
}
Car winerCar = null;
int maxDistance = 0;
for (Car car : cars) {
int distance = car.scoreKillometrs24();
System.out.println("Машина: " + car.getName() + ", Скорость: " + car.getSpeed() + " км/ч" + " Проехала за 24 часа: " + distance);
if (distance > maxDistance) {
maxDistance = distance;
winerCar = car;
}
}
System.out.println("Победила машина: " + winerCar.name);
}
}