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/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Car {
String brand;
int speed;

Car(String brand, int speed) {
this.brand = brand;
this.speed = speed;
}
}
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;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
System.out.println("Приветствую вас на гонке «24 часа Ле-Мана»!");
Scanner scanner = new Scanner(System.in);
Race carLeader = new Race();

for (int i = 1; i <= 3; ++i) {
System.out.println("— Введите название машины №" + i + ":");
String inCarBrand = currectBrand(scanner);
System.out.println("— Введите скорость машины №" + i + ":");
int inCarSpeed = currectSpeed(scanner);
carLeader.makeRace(new Car(inCarBrand, inCarSpeed));
}

if (carLeader.draw == 0) {
System.out.println("Победитель гонки «24 часа Ле-Мана»: " + carLeader.leadRace);
System.out.println("Финишировавшая машина проехала: " + carLeader.leadDistance + " км");
} else {
System.out.println("Победитель гонки «24 часа Ле-Мана» не установлен, так как ничья.");
System.out.println("Максимальное расстояние пройденное за гонку: " + carLeader.leadDistance);
}
}
}

public static int currectSpeed(Scanner scanner1) {
while (true) {
if (scanner1.hasNextInt()) {
int carSpeed = scanner1.nextInt();
scanner1.nextLine();
if (carSpeed >= 0 && carSpeed <= 250) {
return carSpeed;

} else {
System.out.println("Неправильная скорость. Введите скорость в диапозоне от 0 до 250.");

}
} else {
System.out.println("Некорректный ввод. Введите скорость в целочисленном диапозоне от 0 до 250.");
scanner1.nextLine();
}
}
}

public static String currectBrand(Scanner scanner2) {
while (true) {
String carBrand = scanner2.nextLine().trim();
if (!carBrand.isEmpty()) {
return carBrand;
}
else {
System.out.println("Вы оставили поле пустым, попробуйте ввести название машины еще раз: ");
}
}
}

}
17 changes: 17 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Race {
String leadRace = "";
int leadDistance = 0;
int draw = 0;
public void makeRace(Car newCar) {
int time = 24;
int distance = time * newCar.speed;
if (distance == leadDistance) {
draw = -1;
}
if (distance > leadDistance) {
leadRace = newCar.brand;
leadDistance = distance;
draw = 0;
}
}
}