Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,57 @@
import java.util.Scanner;

class Car {
int speed;
String carName;
public Car(String carName, int speed) {
this.carName = carName;
this.speed = speed;
}
}

class Race {
String leaderName;
int distance = 0;
public void takeLeader(Car car) {
int distance = car.speed * 24;
if (this.distance < distance) {
this.leaderName = car.carName;
this.distance = distance;
}
}
}

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);
String carName;
int speed;
Race race = new Race();
for (int i = 1; i <= 3; i++) {
System.out.println("Введите название автомобиля № " + i);
carName = scanner.next();
while (true) {
System.out.println("Введите скорость автомобиля № " + i);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Здесь лучше указать пределы скорости

if (scanner.hasNextInt()) {
speed = scanner.nextInt();
if (speed > 0 && speed <= 250) {
break;
}
else {
System.out.println("Пожалуйста, введите корректную скорость. Повторите ввод!");
}
}
else {
scanner.next();
System.out.println("Пожалуйста, введите целое число. Повторите ввод!");
}

}
Car car = new Car(carName, speed);
race.takeLeader(car);
}
System.out.println("Лидер гонки - автомобиль '" + race.leaderName + "'");
}
}
}