Skip to content
Open
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions src/main/java/Auto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Auto {
final String name;
final int speed;
public Auto(String name, int speed) {
this.name = name;
this.speed = speed;
}

}
34 changes: 31 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@

import java.util.InputMismatchException;
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.println("Введите название машины №" + i);
String name = scanner.next();
int speed;
while (true) {
try {
System.out.print("Введите скорость машины №" + i + "(от 0 до 250 км/ч): ");
speed = scanner.nextInt();

if (speed >= 0 && speed <= 250) {
break;
} else {
System.out.println("Ошибка: Скорость должна быть в диапазоне от 0 до 250 км/ч.");
}
}catch (InputMismatchException e) {
System.out.println("Ошибка: введите корректное число для скорости.");
scanner.next();
}
}
Auto auto = new Auto(name,speed);
race.newLeaderUpdate(auto);

}
System.out.println("Самая быстрая машина: " + race.getCurrentLeader());
}
}
}

20 changes: 20 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class Race {
private String leader = "";
private int leaderDistance = 0;


public void newLeaderUpdate(Auto auto) {
int distance = 24 * auto.speed;

if (distance> leaderDistance){
leaderDistance = distance;
leader = auto.name;
System.out.println("Новый лидер гонки: " + leader + " проехал дистанцию: " + leaderDistance + " км.");
} else {
System.out.println("Лидер по прежнему: " + leader + " проехал дистанцию: " + leaderDistance + " км.");
}
}
public String getCurrentLeader() {
return leader;
}
}