Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
60 lines (48 loc) · 1.83 KB
/
Copy pathMain.java
File metadata and controls
60 lines (48 loc) · 1.83 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Race race = new Race();
for(int i = 0; i < 3; i++) {
System.out.println("Введите характеристики " + (i + 1) + " автомобиля: ");
String name = "";
while (name.isBlank()) {
name = getNamesCar(scanner);
}
int speed = getSpeedCar(scanner);
Car car = new Car(name, speed);
race.getNameLead(car);
}
System.out.println("Самая быстрая машина: " + race.getNameLead());
}
public static String getNamesCar(Scanner scanner){
System.out.println("Название: ");
String input = scanner.nextLine();
if (input.isBlank()) {
System.out.println("Пожайлуста, введите название автомобиля");
}
return input;
}
public static int getSpeedCar(Scanner scanner){
int maxSpeed = 250;
int minSpeed = 0;
int speed = 0;
boolean isValidSpeed = false;
while (!isValidSpeed) {
System.out.println("Скорость: ");
if (scanner.hasNextInt()) {
speed = scanner.nextInt();
scanner.nextLine();
if (speed >= minSpeed && speed <= maxSpeed) {
isValidSpeed = true;
} else {
System.out.println("Ошибка! Значение должно быть от 0 до 250");
}
} else {
System.out.println("Ошибка. Вы ввели некорректное значение");
scanner.nextLine();
}
}
return speed;
}
}