Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
51 lines (50 loc) · 2.04 KB
/
Copy pathMain.java
File metadata and controls
51 lines (50 loc) · 2.04 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) {
Scanner scanner = new Scanner(System.in);
ArrayList<Auto> autoList = new ArrayList<>();
Auto leader;
int autoCount = 3;
for (int i=0; i < autoCount; i++) {
String name;
while (true) {
System.out.println("Введите название " + (i+1) + " автомобиля:");
name = scanner.nextLine().trim();
if (!name.isEmpty()) {
break;
} else {
System.out.println("Некорректно указано название автомобиля");
}
}
int speed;
String speedInput;
while (true) {
System.out.println("Введите скорость " + (i + 1) + " автомобиля:");
speedInput = scanner.nextLine().trim();
boolean isInt = true;
for (int j=0; j < speedInput.length(); j++) {
char c = speedInput.charAt(j);
if (c < '0' || c > '9') {
isInt = false;
}
}
if (speedInput.isEmpty() || !isInt) {
System.out.println("Некорректно указана скорость автомобиля");
} else {
speed = Integer.parseInt(speedInput);
if (speed > 0 && speed <= 250) {
break;
} else {
System.out.println("Некорректно указана скорость автомобиля");
}
}
}
Auto auto = new Auto(name, speed);
autoList.add(auto);
}
leader = Race.identifyLeader(autoList);
System.out.println("Самая быстрая машина: " + leader.name);
scanner.close();
}
}