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
72 lines (51 loc) · 2.27 KB
/
Main.java
File metadata and controls
72 lines (51 loc) · 2.27 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
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
record Car(String name, int speed) {
public double getTimeForDistance(int distanceKm) {
double timeHours = (double) distanceKm / speed;
return timeHours * 3600;
}
public String toString() {
return "Машина: " + name + ", Скорость: " + speed + " км/ч";
}
}
class CarRace {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Car> cars = new ArrayList<>();
final int DISTANCE_KM = 1;
System.out.println("ГОНКА МАШИН");
System.out.println("Введите данные для 3 машин (название и скорость в км/ч, от 1 до 250):");
// Ввод данных для 3 машин
for (int i = 1; i <= 3; i++) {
System.out.println("\nМашина #" + i + ":");
System.out.print("Название машины: ");
String name = scanner.nextLine();
int speed;
do {
System.out.print("Скорость (км/ч, от 1 до 250): ");
speed = scanner.nextInt();
if (speed < 1 || speed > 250) {
System.out.println("Неправильно! Скорость должна быть от 1 до 250 км/ч. Введите заново.");
}
} while (speed < 1 || speed > 250);
scanner.nextLine();
cars.add(new Car(name, speed));
}
System.out.println("\n РЕЗУЛЬТАТЫ ГОНКИ (дистанция: " + (DISTANCE_KM * 1000) + " метров) ");
Car winner = null;
double minTime = Double.MAX_VALUE;
for (Car car : cars) {
double time = car.getTimeForDistance(DISTANCE_KM);
System.out.println(car + " — Время: " + String.format("%.2f", time) + " секунд");
if (time < minTime) {
minTime = time;
winner = car;
}
}
assert winner != null;
System.out.println("\n🏆 ПОБЕДИТЕЛЬ: " + winner.name() + " (время: " + String.format("%.2f", minTime) + " секунд)!");
scanner.close();
}
}