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
10 changes: 10 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Car {
String name;
int speed;

public Car(String name, int speed) {
this.name = name;
this.speed = speed;
}
}

55 changes: 53 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,57 @@
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();

for (int i = 1; i <= 3; i++) {
//Работа с наименованием
String carName = "";
while (true) {

Choose a reason for hiding this comment

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

Не рекомендую писать бесконечные циклы через while (true) - лучше всегда явно прописывать условие выхода из цикла, чтобы уменьшить вероятность ошибиться и повысить читабельность кода

System.out.println(" - Введите название машины № " + i);
carName = scanner.nextLine();
if (carName.isBlank()) {

Choose a reason for hiding this comment

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

Код для считывания непустой строки с ввода лучше вынести в отдельную функцию - код, разделённый на небольшие функции, легче читать, поддерживать и переиспользовать

System.out.println("Вы не ввели название машины!");
} else {
break;
}
}
//Работа со скоростью
int speed;
while (true) {
System.out.println(" - Введите скорость машины № " + i);
String input = scanner.nextLine();

Choose a reason for hiding this comment

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

Код для считывания скорости с ввода лучше вынести в отдельную функцию аналогично


if (input.isBlank()) {
System.out.println("Вы ввели пустую строчку!");
continue;
}
try {
speed = Integer.parseInt(input);
if (speed <= 250 && speed > 0) {
break;
} else {
System.out.println("Введена некорректная скорость! Введите скорость из диапазона от 0 до 250");
}
} catch (NumberFormatException e) {
System.out.println("Введите целое число!");
}
}
Car car = new Car(carName, speed);
race.getDistation(car);
}
scanner.close();
System.out.println("Самая быстрая машина: " + race.leader);
}
}
}










14 changes: 14 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Race {
String leader = "";
int leaderDistance = 0;
int time = 24;
public void getDistation(Car newCar) {
int distance = time * newCar.speed;
if (distance > leaderDistance) {
leader = newCar.name;
leaderDistance = distance;
}
}
}