Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
56 lines (47 loc) · 1.93 KB
/
Copy pathMain.java
File metadata and controls
56 lines (47 loc) · 1.93 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Gonka autoGonka = new Gonka();
int i=1;
while (i<=3){
String autoName = askAutoName(scanner, i);
int autoSpeed = askSpeed(scanner, i);
Auto newAuto = new Auto(autoName, autoSpeed);
autoGonka.findWinner(newAuto);
i++;
}
scanner.close();
String winnersGonka = autoGonka.winnersName();
System.out.println("Самый быстрый автомобиль: " + winnersGonka);
}
static String askAutoName(Scanner scanner, int i){
String autoName = "";
while (autoName.isEmpty()) {
System.out.println("Введите название автомобиля №" + i + ":");
autoName = scanner.next();
if (autoName.isEmpty()) {
System.out.println("Введено пустое название автомобиля №" + i + ". Повторите ввод.");
scanner.skip(".*\n");
}
}
return autoName;
}
static int askSpeed(Scanner scanner, int i){
int autoSpeed = 0;
while (autoSpeed<=0 || autoSpeed>250){
scanner.skip(".*\n");
System.out.println("Введите скорость автомобиля №" + i + ":");
if (scanner.hasNextInt()) {
autoSpeed = scanner.nextInt();
if (autoSpeed<=0 || autoSpeed>250){
System.out.println("Скорость автомобиля №" + i + " должна быть от 0 до 250.");
}
}
else{
System.out.println("Скорость автомобиля №" + i + " должна содержать только целые числа.");
}
}
return autoSpeed;
}
}