-
Notifications
You must be signed in to change notification settings - Fork 959
Expand file tree
/
Copy pathMain.java
More file actions
64 lines (49 loc) · 1.65 KB
/
Main.java
File metadata and controls
64 lines (49 loc) · 1.65 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = 3;
Car[] cars = new Car[n];
for (int i = 0; i < n; i++) {
System.out.println("Введите название машины №" + (i + 1) + ":");
String name = scanner.nextLine();
double velocity;
while (true) {
System.out.println("— Введите скорость машины №" + (i + 1) + ":");
velocity = scanner.nextDouble();
scanner.nextLine();
if (velocity >= 0 && velocity <= 250) break;
System.out.println("Неправильная скорость");
}
cars[i] = new Car(name, velocity);
}
Car winner = Race.winner(cars);
if (winner == null) {
System.out.println("Нет машин с допустимой скоростью (0..250).");
} else {
System.out.println("Самый быстрый автомобиль: " + winner.name);
}
scanner.close();
}
}
class Car {
String name;
double velocity;
Car(String name, double velocity) {
this.name = name;
this.velocity = velocity;
}
}
class Race {
static Car winner(Car[] cars) {
Car winner = null;
for (Car c : cars) {
if (c.velocity >= 0 && c.velocity <= 250) {
if (winner == null || c.velocity * 24 > winner.velocity * 24) {
winner = c;
}
}
}
return winner;
}
}