Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
51 lines (43 loc) · 1.96 KB
/
Copy pathMain.java
File metadata and controls
51 lines (43 loc) · 1.96 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
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int numberOfСars = 3; //количество машин участвующих в гонке
ArrayList<Car> listOfCar = new ArrayList<>();
Scanner sr = new Scanner(System.in);
for (int i = numberOfСars; i > 0; i--) {
String carName;
while (true) {
System.out.println("Введите название машины №" + (numberOfСars - i + 1));
carName = sr.nextLine();
if (carName != null && !carName.isEmpty() && !carName.trim().isEmpty()) {
break;
} else System.out.println("Введено не корректное значение пропробуйте снова");
}
int carSpeed;
while (true) {
System.out.println("Введите скорость машины №" + (numberOfСars - i + 1));
String carSpeedString = sr.nextLine();
if (isNumber(carSpeedString)) {
if (Integer.parseInt(carSpeedString) > 0 && Integer.parseInt(carSpeedString) <= 250) {
carSpeed = Integer.parseInt(carSpeedString);
break;
} else System.out.println("Введено не корректное значение пропробуйте снова");
} else System.out.println("Введено не корректное значение пропробуйте снова");
}
Car car = new Car(carName, carSpeed);
listOfCar.add(car);
}
Race race = new Race(listOfCar);
race.resultRace();
sr.close();
}
public static boolean isNumber(String str) {
try {
Integer.parseInt(str);
} catch (Exception e) {
return false;
}
return true;
}
}