-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathMain.java
More file actions
81 lines (61 loc) · 2.05 KB
/
Copy pathMain.java
File metadata and controls
81 lines (61 loc) · 2.05 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.util.Scanner;
public class Main {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
Race race = new Race();
for (int i = 1; i < 4; i++ )
{
Car car = readCar(i);
race.addPlayer(car.name, car.speed);
}
System.out.println("Самая быстрая машина: " + race.leedName);
}
private static Car readCar(int number)
{
System.out.println("Введите название авто № " + number);
String name;
while (true) {
name = scanner.next();
if (checkName(name)) {
break;
}
}
int speed;
System.out.println("Введите скорость авто № " + number);
while (true) {
speed = checkSpeed(scanner.next());
if (speed > 0) {
break;
}
}
return new Car(name, speed);
}
private static Boolean checkName(String name)
{
if (name.trim().isEmpty()) {
System.out.println("Название автомобиля не должно быть пустым, повторите попытку:");
return false;
}
return true;
}
private static int checkSpeed(String speed)
{
int number = -1;
if (speed.trim().isEmpty()) {
System.out.println("Скорость не должна быть пустым значением, повторите попытку:");
return 0;
}
try {
number = Integer.parseInt(speed);
} catch (NumberFormatException e) {
System.out.println("Некорректное число, повторите попытку: ");
return -1;
}
if (number > 0 && number < 250)
{
return number;
}
System.out.println("Ожидается скорость > 0 и < 250, повторите попытку: ");
return -1;
}
}