Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
61 lines (49 loc) · 2.09 KB
/
Copy pathMain.java
File metadata and controls
61 lines (49 loc) · 2.09 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
61
import java.util.Scanner;
public class Main {
private static final int NUM_CARS = 3;
private static final int MIN_SPEED = 0;
private static final int MAX_SPEED = 250;
public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Добро пожаловать на гонку \"24 часа Ле-Мана\"!");
Race race = new Race();
Scanner in = new Scanner(System.in);
for (int i = 0; i < NUM_CARS; i++) {
String name = "";
int speed = -1;
while (name.isEmpty()) {
System.out.println("— Введите название машины №" + (i + 1) + ":");
name = scanName(in.nextLine());
if (!name.isEmpty()) break;
}
while (speed == -1) {
System.out.println("— Введите скорость машины №" + (i + 1) + ":");
speed = scanSpeed(in.nextLine());
if (speed != -1) break;
}
Car car = new Car(name, speed);
race.compete(car);
}
race.result();
}
public static String scanName(String str) {
String strName = str.trim();
if (strName.isEmpty())
System.out.println("— Название не должно быть пустым");
return strName;
}
public static int scanSpeed(String str) {
if (str.matches("\\d+")) { //введена одна или больше цифр
int speed = Integer.parseInt(str);
if (speed < MIN_SPEED || speed > MAX_SPEED) {
System.out.println("— Неправильная скорость");
return -1;
}
return speed;
} else {
System.out.println("— Неправильная скорость");
}
return -1;
}
}