Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
51 lines (41 loc) · 1.64 KB
/
Copy pathMain.java
File metadata and controls
51 lines (41 loc) · 1.64 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.Scanner;
public class Main {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
Race race = new Race();
System.out.println("Добро пожаловать на гонку Леман 24!");
for (int i = 1; i <= 3; i++) {
String carName = getName(i);
int carSpeed = getSpeed(i);
race.addCar(new Car(carSpeed, carName));
}
System.out.println("Самая быстрая машина: " + race.getWinnerName());
}
public static String getName(int carNumber) {
String name = "";
while (name.isEmpty()) {
System.out.println("Введите название машины №" + carNumber);
name = scanner.next();
}
return name;
}
public static int getSpeed(int carNumber) {
final int MIN_SPEED = 0, MAX_SPEED = 250;
System.out.println("Введите скорость машины №" + carNumber);
while (true) {
// SI (super intelligence) подсказал наличие метода hasNextInt
if (scanner.hasNextInt()) {
int speed = scanner.nextInt();
boolean isValid = speed > MIN_SPEED && speed <= MAX_SPEED;
if (isValid) {
return speed;
} else {
System.out.println("Допустима скорость от 1 до 250 км/ч");
}
} else {
System.out.println("Введите целое число");
scanner.next();
}
}
}
}