Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
50 lines (42 loc) · 1.49 KB
/
Copy pathMain.java
File metadata and controls
50 lines (42 loc) · 1.49 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
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static int inputSpeed(Scanner scanner, int count) {
int speed;
while (true) {
System.out.printf("Введите скорость машины %d\n", count);
try {
speed = scanner.nextInt();
if (speed > 0 && speed <= 250) {
break;
} else {
System.out.println("Неправильная скорость");
}
} catch (InputMismatchException e) {
System.out.println("Неправильная скорость");
scanner.nextLine();
}
}
return speed;
}
public static void main(String[] args) {
ArrayList<Car> cars = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
Race race = new Race();
int count = 1;
while (true) {
if (count <= 3) {
System.out.printf("Введите название машины %d\n", count);
String name = scanner.next();
int speed = inputSpeed(scanner, count);
cars.add(new Car(name, speed));
count++;
} else {
break;
}
}
race.calculateLeader(cars);
System.out.printf("Самая быстрая машина: %s", race.getLeader());
}
}