From 44c41ec371a26b5fdef8d71f127598d112410ae3 Mon Sep 17 00:00:00 2001 From: Danil Date: Wed, 27 Nov 2024 16:55:48 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9A=D0=BE=D0=BC=D0=B8=D1=82=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D0=B0=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 90 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a0..4b13232a 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,94 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +class Car { + private String name; + private double speed; + + public Car(String name, double speed) { + this.name = name; + this.speed = speed; + } + + public String getName() { + return name; + } + + public double getSpeed() { + return speed; + } + + public double distance() { + return speed * 24; + } +} + +class Race { + private List cars; + + public Race() { + cars = new ArrayList<>(); + } + + public void addCar(Car car) { + cars.add(car); + } + + public Car Leader() { + if (cars.isEmpty()) { + return null; + } + Car leader = cars.get(0); + for (Car car : cars) { + if (car.distance() > leader.distance()) { + leader = car; + } + } + return leader; + } +} public class Main { + private static Car getCarData() { + Scanner scanner = new Scanner(System.in); + String name; + double speed; + + while (true) { + System.out.print("Введите название автомобиля: "); + name = scanner.nextLine(); + + System.out.print("Введите скорость автомобиля: "); + String speedInput = scanner.nextLine(); + + try { + if (speedInput.contains(".")) { + throw new NumberFormatException("Число с плавающей точкой не допустимо."); + } + speed = Double.parseDouble(speedInput); + if (speed > 0 && speed <= 250) { + return new Car(name, speed); + } else { + System.out.println("Неправильная скорость"); + } + } catch (NumberFormatException e) { + System.out.println("Пожалуйста, введите корректное число для скорости."); + } + } + } + public static void main(String[] args) { - System.out.println("Hello world!"); + Race race = new Race(); + + for (int i = 0; i < 3; i++) { + Car car = getCarData(); + race.addCar(car); + } + + Car leader = race.Leader(); + if (leader != null) { + System.out.println("Самая быстрая машина: " + leader.getName()); + } } } \ No newline at end of file From 0507bb799e8c093115f530c21aa957c5cf9bae86 Mon Sep 17 00:00:00 2001 From: Danil Date: Wed, 27 Nov 2024 16:57:21 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=9A=D0=BE=D0=BC=D0=B8=D1=82=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D0=B0=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 4b13232a..756e82f6 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -70,7 +70,7 @@ private static Car getCarData() { if (speed > 0 && speed <= 250) { return new Car(name, speed); } else { - System.out.println("Неправильная скорость"); + System.out.println("Неправильная скорость автомобиля"); } } catch (NumberFormatException e) { System.out.println("Пожалуйста, введите корректное число для скорости.");