From ea3be7255a2e89f026dc01dbc7ed475485f731f7 Mon Sep 17 00:00:00 2001 From: ryblis Date: Thu, 24 Oct 2024 20:00:13 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=801?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Car.java | 9 +++++++++ src/main/java/Main.java | 24 +++++++++++++++++++++++- src/main/java/Race.java | 17 +++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/main/java/Car.java create mode 100644 src/main/java/Race.java diff --git a/src/main/java/Car.java b/src/main/java/Car.java new file mode 100644 index 00000000..adf3bf8b --- /dev/null +++ b/src/main/java/Car.java @@ -0,0 +1,9 @@ +public class Car { + public String name; // Название автомобиля + public int speed; // Скорость автомобиля + + + public Car(String name, int speed) { + this.name = name; + this.speed = speed;} +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a0..e5fbdc30 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,28 @@ +import java.util.Scanner; public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + Scanner scanner = new Scanner(System.in); + Race race = new Race(); + + for (int i = 1; i <= 3; i++) { + System.out.print("Введите название машины №" + i + ": "); + String name = scanner.next(); + + int speed; + while (true) { + System.out.print("Введите скорость машины №" + i + ": "); + speed = scanner.nextInt(); + if (speed > 0 && speed <= 250) { + break; + } else { + System.out.println("Неправильная скорость. Пожалуйста, введите скорость от 1 до 250."); + } + } + Car car = new Car(name, speed); + race.determineNewLeader(car); + } + System.out.println("Самая быстрая машина: " + race.getCurrentLeader()); + scanner.close(); } } \ No newline at end of file diff --git a/src/main/java/Race.java b/src/main/java/Race.java new file mode 100644 index 00000000..e7b0ea02 --- /dev/null +++ b/src/main/java/Race.java @@ -0,0 +1,17 @@ +public class Race { + String currentLeader = ""; // Текущий лидер гонки + int leaderDistant = 0; // Дистанция, пройденная текущим лидером + + public void determineNewLeader(Car car) { + int timeRace = 24; + int newDistance = timeRace * car.speed; + + if (newDistance > leaderDistant) { + currentLeader = car.name; + leaderDistant = newDistance; + } + } + public String getCurrentLeader() { + return currentLeader; + } +} \ No newline at end of file