Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
55 lines (48 loc) · 1.8 KB
/
Copy pathMain.java
File metadata and controls
55 lines (48 loc) · 1.8 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
import java.util.Scanner;
public class Main {
static final int MAX_SPEED = 250;
static final int MIN_SPEED = 0;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Race race = new Race();
for (int i = 1; i <= 3; i++) {
String name = nonEmptyString(scanner, i);
int speed = readSpeed(scanner, i);
race.newLeader(name, speed);
}
System.out.println("Самая быстрая машина: " + race.leaderName);
scanner.close();
}
static String nonEmptyString(Scanner scanner, int carsNumb) {
String name;
do {
System.out.println("Введите название машины №" + carsNumb + ": ");
name = scanner.nextLine().trim();
if (name.isEmpty()) {
System.out.println("Название не может быть ПУСТЫМ");
}
} while (name.isEmpty());
return name;
}
static int readSpeed(Scanner scanner, int carsNumb) {
int speed = 0;
boolean isValid;
do {
isValid = false;
System.out.println("Введите скорость машины №" + carsNumb + ": ");
if (scanner.hasNextInt()) {
speed = scanner.nextInt();
scanner.nextLine();
if (speed >= MIN_SPEED && speed <= MAX_SPEED) {
isValid = true;
} else {
System.out.println("Неправильная скорость ");
}
} else {
System.out.println("Введите ЦЕЛОЕ число!");
scanner.next();
}
} while (!isValid) ;
return speed;
}
}