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
66 changes: 64 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,68 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Car[] cars = new Car[3];
Race race = new Race();

Scanner scanner = new Scanner(System.in);

for (int i = 0; i < cars.length; i++) {
String name;
System.out.println("Введите название машины №" + (i + 1));
while (true) {
name = scanner.nextLine();
if (!name.isEmpty()) {
break;
} else {
System.out.println("У каждой машины есть название! Тут не может быть пусто :(");
}
}

int speed;
System.out.println("Введите скорость машины №" + (i + 1));
while (true) {
// Столкнулся с проблемой проверки пустого ввода и значений "abc/#$%" или "2.50" в int
// Решение через try/catch подсказал ИИ, так как способы проверки вводимых данных через scanner на курсе ещё не проходили :)
Comment on lines +25 to +26

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

try {
speed = Integer.parseInt(scanner.nextLine());
if (speed > 0 && speed <= 250) {
cars[i] = new Car(name, speed);
race.choosingLeader(cars[i]);
break;
} else {
System.out.println("Введена неверная скорость, введите скорость от 0 до 250!");
}
} catch (NumberFormatException e) {
System.out.println("Введите целочисленное число!");
}
}
}

System.out.println("Самая быстрая машина " + race.raceLeader);

}
}
}

class Car {
String carName;
int speed;
Car(String carName, int speed) {
this.carName = carName;
this.speed = speed;
}
}

class Race {
String raceLeader = "";
int leaderDistance = 0;
public void choosingLeader (Car cars) {
int distance = 24 * cars.speed;

if (distance > leaderDistance) {
raceLeader = cars.carName;
leaderDistance = distance;
}
}
}