Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
81 lines (65 loc) · 2.13 KB
/
Copy pathMain.java
File metadata and controls
81 lines (65 loc) · 2.13 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 {
public static void main(String[] args) {
Car[] cars = input2();
Race race = new Race(cars);
Car winner = race.winner();
System.out.println("Самая быстрая машина: " + winner.name);
}
private static Car[] input2() {
Scanner scanner = new Scanner(System.in);
Car[] cars = new Car[3];
for (int i = 0; i < 3; i++) {
System.out.println("Введите название машины #" + (i + 1) + ": ");
String name = "";
while (scanner.hasNextInt()) {
System.out.println("Ошибка: введите не число");
scanner.next();
}
name = scanner.next();
int speed = 0;
while (true) {
System.out.println("Введите скорость автомобиля #" + (i + 1) + " (км/ч): ");
scanner.nextLine();
if (!scanner.hasNextInt()) {
System.out.println("Ошибка: введите не строку");
continue;
}
speed = scanner.nextInt();
if (speed > 0 && speed <= 250) {
break;
} else {
System.out.println("Ошибка: скорость должна быть больше 0 и не более 250. Попробуйте снова.");
}
}
cars[i] = new Car(name, speed);
}
return cars;
}
}
class Car {
String name;
int speed;
Car(String name, int speed) {
this.name = name;
this.speed = speed;
}
}
class Race {
Car[] cars;
Race(Car[] cars) {
this.cars = cars;
}
public Car winner() {
Car winner = null;
int maxDistance = 0;
for (Car current : cars) {
int distance = current.speed * 24;
if (distance > maxDistance) {
maxDistance = distance;
winner = current;
}
}
return winner;
}
}