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
10 changes: 10 additions & 0 deletions src/main/java/Cars.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class Cars {
String modelCar; // модель авто
int speedCar; // скорость авто

// Конструктор
Cars (String modelCar, int speedCar){
this.modelCar = modelCar;
this.speedCar = speedCar;
}
}
40 changes: 39 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
import java.util.Scanner;

public class Main {
public static final String pusto = "-------------------------------------\n"; // Разделитель

public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in); // Создаём экземпляр класса Scanner.

int maxCar = 3; // max авто
Cars[] cars = new Cars[maxCar]; // массив авто
Race race = new Race(); //объект Race

//Запрашиваем у пользователя 3 автомобиля
for (int i = 0; i < maxCar; i++) {
System.out.printf(Main.pusto + "— Введите название машины № %d: ", i + 1);
String model = scanner.nextLine();


//Проверяем, что введённая скорость >0 и ⩽250
int speed = 0;
while (true) {
System.out.printf("— Введите скорость машины № %d: ", i + 1);

if (scanner.hasNextInt()) {
speed = scanner.nextInt();
scanner.nextLine(); // очищаем отступ после nextInt
if (speed > 0 && speed <= 250) {
break;
} else {
System.out.println("— Неправильная скорость");
}
} else {
System.out.println("— Введите ниже целое число");
scanner.next(); // очищаем неверный ввод
}
}
cars[i] = new Cars(model, speed); // Добавляем в массив новую машину
}


System.out.println(Main.pusto);
race.resultRace(cars);
}
}
24 changes: 24 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class Race {
void resultDay(Cars cars){
int resultForAllCars = cars.speedCar * 24;
System.out.printf("Машина [%s] проедет [%d] км за сутки.\n", cars.modelCar, resultForAllCars);
}
void resultRace(Cars[] cars) {
Cars leader = null;
int maxDistance = 0;

System.out.println("\nРезультаты гонки:");
for (Cars car : cars) {
resultDay(car);
int distance = car.speedCar * 24;
if (distance > maxDistance) {
maxDistance = distance;
leader = car;
}
}

if (leader != null) {
System.out.println("\nСамая быстрая машина: " + leader.modelCar);
}
}
}