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
85 changes: 84 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,89 @@
import java.util.ArrayList;
import java.util.Scanner;
class Car {
String name;
int speed;

public Car(String name, int speed){
this.name = name;
this.speed = speed;
}
public String toString() {
return "Машина '" + name + "', скорость = " + speed;
}
}
class Race {
String name;

public String findRaceWinner(ArrayList<Car> listCars){
int bestResult = 0;
String carName = null;
for(int i = 0; i<3; i++){
Car car = listCars.get(i);
int length = car.speed * 24;
if(bestResult < length){
bestResult = length;
carName = car.name;
}
}
this.name = carName;
return name;
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);
ArrayList<Car> listCars = new ArrayList<>();

for (int i = 1; i <= 3; i++) {
String carName = getCarName(scanner, i);
int speed = getCarSpeed(scanner, i);

listCars.add(new Car(carName, speed));
}

for (Car car : listCars) {
System.out.println(car);
}

Race race = new Race();
String winner = race.findRaceWinner(listCars);

System.out.println("Победитель гонки машина - '" + winner + "'");
}

private static String getCarName(Scanner scanner, int carNumber) {
String carName;
while (true) {
System.out.println("Введите название " + carNumber + " автомобиля:");
carName = scanner.nextLine().trim();
if (carName.isEmpty()) {
System.out.println("Пустое значение имени, попробуйте снова.");
} else {
break;
}
}
return carName;
}

private static int getCarSpeed(Scanner scanner, int carNumber) {
int speed;
while (true) {
System.out.println("Введите скорость " + carNumber + " автомобиля:");

if (!scanner.hasNextInt()) {
System.out.println("Неправильное числовое значение, попробуйте снова.");
scanner.next();
continue;
}
speed = scanner.nextInt();
scanner.nextLine();
if (speed <= 0 || speed > 250) {
System.out.println("Некорректное значение скорости (скорость должна быть в диапазоне от 0 до 250 км/ч). Попробуйте снова.");
} else {
break;
}
}
return speed;
}
}