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
8 changes: 8 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public class Car {
String brand;
int speed;
public Car(String brand, int speed) {
this.brand = brand;
this.speed = speed;
}
}
46 changes: 44 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,48 @@
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();

// указываем данные для 3ех автомобилей
for (int i = 1; i <= 3; i++) {
Car car = new Car("", 0);

// проверка на корректность названия
while (true) {
System.out.println("— Введите название машины №" + i + " :");
car.brand = scanner.nextLine();
if (car.brand.isEmpty()) {
System.out.println("— Пустое название автомобиля");
} else {
break;
}
}

// проверка на корректность скорости
while (true) {
System.out.println("— Введите скорость машины №" + i + " :");
if (scanner.hasNextInt()) {
car.speed = scanner.nextInt();
scanner.nextLine(); //очищаем буффер
if (car.speed <= 0 || car.speed > 250) {
System.out.println("— Неправильная скорость");
} else {
break;
}
}
else {
scanner.nextLine(); //очищаем буффер
System.out.println("— Неправильная скорость");
}
}

// если у нескольких участкиков одинаковая скорость, лидером будет та машина, скорость которой указана раньше
race.getNewWinner(car);
}
scanner.close();
System.out.println("Самая быстрая машина: " + race.currentWinner);
}
}
}

10 changes: 10 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class Race {
int distance = 0;
String currentWinner = "";
public void getNewWinner (Car car) {
if (car.speed * 24 > distance) {
distance = car.speed * 24;
currentWinner = car.brand;
}
}
}