Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
54 lines (51 loc) · 2.07 KB
/
Copy pathMain.java
File metadata and controls
54 lines (51 loc) · 2.07 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Car car = new Car();
Race race = new Race();
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
int carNum = i+1;
car = addCarbyNumber(carNum, scanner);
race.identifyLeader(car.name, car.speed);
}
System.out.print("Самая быстрая машина: " + race.leader);
scanner.close();
}
public static Car addCarbyNumber(int carNumber, Scanner scanner){
Car car = new Car();
String name = "";
int speed;
while (true) {
System.out.println(" - Введите название машины № " + carNumber + ":");
name = scanner.nextLine();
if (name.trim().isEmpty()) {
System.out.println("Название машины не введено!");
} else {
car.name = name;
break;
}
}
while (true) {
System.out.println(" - Введите скорость машины № " + carNumber + ":");
String input = scanner.nextLine().trim();
if (input.isEmpty()) {
System.out.println("Ошибка: значение скорости не введено!");
} else {
try {
speed = Integer.parseInt(input); //как парсить тоже спросила в Яндексе
if (speed > 0 && speed <= 250) {
car.speed = speed;
break;
} else {
System.out.println("Ошибка! Значение скорости должно быть больше 0 и меньше 250.");
}
}
catch(NumberFormatException e) {
System.out.println("Ошибка! Введите целое число от 1 до 250!");
}
}
}
return car;
}
}