forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
74 lines (61 loc) · 2.16 KB
/
Main.java
File metadata and controls
74 lines (61 loc) · 2.16 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
import java.util.Scanner;
class Car {
private String make;
private int speed;
public Car(String make, int speed) {
this.make = make;
this.speed = speed;
}
public double calculateDistance(){
return speed * 24.0;
}
public String getMake(){
return make;
}
}
class Race {
private Car[] participants;
public Race(Car[] participants) {
this.participants = participants;
}
public Car determineWinner() {
Car leader = participants[0];
for (int i = 1; i < participants.length; i++) {
if (participants[i].calculateDistance() > leader.calculateDistance()) {
leader = participants[i];
}
}
return leader;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Car[] cars = new Car[3];
for (int i = 0; i < 3; i++) {
System.out.println("Введите данные для автомобиля " + (i + 1) + ":");
System.out.print("Название: ");
String make = scanner.nextLine();
int speed = 0;
boolean validSpeed = false;
while (!validSpeed) {
System.out.print("Скорость (1-250 км/ч): ");
try {
speed = Integer.parseInt(scanner.nextLine());
if (speed > 0 && speed <= 250) {
validSpeed = true;
} else {
System.out.println("Ошибка: скорость должна быть от 1 до 250 км/ч!");
}
} catch (NumberFormatException e) {
System.out.println("Ошибка: введите целое число!");
}
}
cars[i] = new Car(make, speed);
System.out.println("---");
}
Race race = new Race(cars);
Car winner = race.determineWinner();
System.out.println("Победитель гонки '24 часа Ле-Мана': " + winner.getMake());
}
}