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
50 changes: 29 additions & 21 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
import java.util.HashMap;
import java.util.Scanner;

public class Main {
// директиву final и константы мы еще в рамках курса не проходили
// но не могу не воспользоваться советом куратора )
// вообще весь код можно было сделать гораздо проще, но хочется
// попробовать разные возможности языка
// Вынес что возможно в константы
public static final int minSpeed = 0;
public static final int maxSpeed = 250;
public static final int racingTime = 24;


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Создадим переменную для регистрации участников гонки
//Car[] listOfParticipants = new Car[3];

// Регистрация участников
// Можно сразу при регистрации участников определить победителя,
// как советует Artur, но тогда пропадает необходимость в классе Race
// который вроде как нужен по условиям задачи
// При регистрации участников сразу вычисляем победителя гонок
Car leader = null;
int maxDistance = 0;


for (int i = 0; i < 3; i++) {
int numberOfParticipant = i + 1;
System.out.println(String.format("Введите имя %d участника", numberOfParticipant));
String currentName = scanner.next();

System.out.println("Введите имя " + numberOfParticipant + " участника");
String currentName;
while ((currentName = scanner.nextLine()).trim().isEmpty()) {
System.out.println("Вы ввели некорректное имя участника!");
}
int currentSpeed = inputSpeed(scanner, numberOfParticipant);
scanner.nextLine();

Car currentCar = new Car(currentName, currentSpeed);
int currentDistance = currentCar.getDistance(racingTime);
Expand All @@ -42,18 +37,31 @@ public static void main(String[] args) {
}
}
}

System.out.println("В гонке победил автомобиль " + leader.name);
scanner.close();
}

// Вынес код ввода скорости в отдельную функцию
public static int inputSpeed(Scanner scanner, int numberOfParticipant) {
int currentSpeed = -1;
while (currentSpeed < minSpeed || currentSpeed > maxSpeed) {
System.out.println(String.format("Введите скорость автомобиля %d участника в диапазоне от %d до %d км/ч", numberOfParticipant, minSpeed, maxSpeed));
currentSpeed = scanner.nextInt();

System.out.println("Введите скорость автомобиля " + numberOfParticipant + " участника в диапазоне от " + minSpeed + " до " + maxSpeed + " км/ч");
while (true) {
if (!scanner.hasNextInt()) {
System.out.println("Вы ввели что-то не то!");
// что-то без этого уходит в бесконечный цикл, наверное нужно ввод как-то прочитать
scanner.nextLine();
} else {
currentSpeed = scanner.nextInt();
if (currentSpeed <= minSpeed || currentSpeed >= maxSpeed) {
System.out.println("Введите значение скорости в диапазоне от " + minSpeed + " до " + maxSpeed + " км/ч");
scanner.nextLine();
} else {
break;
}
}
}

return currentSpeed;
}
}

}