import java.util.InputMismatchException; import java.util.Scanner; public class Race { private final static int MAX_SPEED = 250; private final static int MIN_SPEED = 0; private final static Scanner SCANNER = new Scanner(System.in); private static Car winner = new Car("", -1); public static void startRace() { System.out.println("ÐобÑо пожаловаÑÑ Ð½Ð° гонки! ТÑебÑеÑÑÑ Ð²Ð²ÐµÑÑи 3 авÑомобилÑ."); for (int i = 1; i < 4; i++) { System.out.println("======================="); System.out.println("ÐведиÑе название Ð´Ð»Ñ " + i + " авÑомобилÑ"); String name = requestName(); int speed = requestSpeed(); Car car = new Car(name, speed); winner = (winner.getKm() > car.getKm()) ? winner : car; } outputWinner(winner); } private static boolean checkCorrectName(String name) { return name.isBlank(); } private static boolean checkCorrectSpeed(int speed) { return speed < MIN_SPEED || speed > MAX_SPEED; } private static String requestName() { String name; System.out.print("Ðазвание: "); name = SCANNER.nextLine(); while (true) { if (checkCorrectName(name)) { System.out.println("Ðведена пÑÑÑÐ°Ñ ÑÑÑока. ÐовÑоÑиÑе Ñнова!"); System.out.print("ÐовÑоÑнÑй ввод названиÑ: "); name = SCANNER.nextLine(); } else { break; } } return name; } private static int requestSpeed() { int speed; System.out.println("ÐведиÑе ÑкоÑоÑÑÑ"); System.out.print("CкоÑоÑÑÑ (Ñелое ÑиÑло Ð¾Ñ 0 до 250): "); try { speed = SCANNER.nextInt(); SCANNER.nextLine(); while (true) { if (checkCorrectSpeed(speed)) { System.out.println("Ðведена не веÑÐ½Ð°Ñ ÑкоÑоÑÑÑ. Ðиапазон Ð¾Ñ 0 до 250"); System.out.print("Ðовое знаÑение ÑкоÑоÑÑи: "); speed = SCANNER.nextInt(); } else { break; } } } catch (InputMismatchException e) { System.out.println("ÐШÐÐÐÐ: СкоÑоÑÑÑ Ð½Ðµ Ð¼Ð¾Ð¶ÐµÑ Ð±ÑÑÑ Ð±Ñквой или дÑобÑÑ"); System.out.println("-----------------"); SCANNER.nextLine(); speed = requestSpeed(); } return speed; } private static void outputWinner(Car car) { System.out.println("-------------------"); System.out.printf("СамÑй лÑÑÑий и бÑÑÑÑÑй авÑомобилÑ: %s", car.getNAME()); SCANNER.close(); } }