-
Notifications
You must be signed in to change notification settings - Fork 959
Expand file tree
/
Copy pathMain.java
More file actions
60 lines (50 loc) · 2.06 KB
/
Main.java
File metadata and controls
60 lines (50 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
58
59
60
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Race race = new Race();
Scanner scanner = new Scanner(System.in);
ArrayList<Car> cars = new ArrayList<>();
int raceParticipants;
while (true) {
System.out.println("Введите количество участников: ");
if (!scanner.hasNextInt()) {
System.out.println("Ошибка ввода");
scanner.next();
}
else {
raceParticipants = scanner.nextInt();
scanner.nextLine();
break;
}
}
for (int i = 1; i <= raceParticipants; i++) {
System.out.println(String.format("Введите название %d машины: ", i));
String name = scanner.nextLine();
while (true){
System.out.println(String.format("Введите скорость %d машины: ", i));
if (!scanner.hasNextInt()){
System.out.println("Ошибка - Скорость должна быть целым числом");
scanner.next();
continue;
}
int speed = scanner.nextInt();
scanner.nextLine();
if (speed > 0 && speed <= 250){
Car car = new Car(name, speed);
cars.add(car);
break;
}
else {
System.out.println("Ошибка - Скорость должна быть выше 0 и меньше 251");
}
}
}
System.out.println("Список участников гонки: ");
for (Car getCar : cars) {
System.out.println(String.format("Название - %s; Скорсть - %d", getCar.name, getCar.speed));
}
Car winner = race.getLeader(cars);
System.out.println("Победитель " + winner.name);
}
}