Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
48 lines (43 loc) · 2.08 KB
/
Copy pathMain.java
File metadata and controls
48 lines (43 loc) · 2.08 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
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Car> cars = new ArrayList<>();
for (int i = 0; i < 3; i++) {
System.out.println("Введите название автомобиля №" + (i + 1) + ":");
String carName = scanner.nextLine();
carName = carName.trim();
while (carName.isEmpty()) {
System.out.println("Введена пустая строка. Введите название автомобиля №" + (i + 1) + " повторно:");
carName = scanner.nextLine();
carName = carName.trim();
}
System.out.println("Введите скорость автомобиля №" + (i + 1) + ":");
int speed;
while (true) {
String strSpeed = scanner.nextLine();
strSpeed = strSpeed.trim();
if (strSpeed.isEmpty()) {
System.out.println("Введена пустая строка. Введите скорость автомобиля №" + (i + 1) + " повторно:");
continue;
}
try {
speed = Integer.parseInt(strSpeed);
} catch (NumberFormatException ex) {
System.out.println("Введено не число. Введите скорость автомобиля №" + (i + 1) + " повторно:");
continue;
}
if (speed <= 0 || speed > 250) {
System.out.println("Скорость вне возможного диапазона (0;250]. Введите скорость автомобиля №" + (i + 1) + " повторно:");
continue;
}
break;
}
Car car = new Car(carName, speed);
cars.add(car);
}
Race race = new Race();
race.calcWinner(cars);
}
}