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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Поля лучше пометить final, тем самым исключив возможность их модификации извне

Car(String name, int speed) {
this.name = name;
this.speed = speed;
}
}
41 changes: 40 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,45 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
System.out.println("Добро пожаловать на гонку!");
Scanner scanner = new Scanner(System.in);

Race race = new Race();

for (int i = 1; i <= 3; i++) {
String name;
int speed;

while (true) {

Copy link
Copy Markdown

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 + ":");
name = scanner.next();

if (name.isBlank()) {
System.out.println("Укажите марку машины!");
} else {
while (true) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

System.out.println("— Введите скорость машины №" + i + ":");
if (scanner.hasNextInt()) {
speed = scanner.nextInt();
if (speed <= 0 || speed > 250) {
System.out.println("Укажите скорость от 0 до 250!");
} else {
break;
}
} else {
System.out.println("Введите значение!");
scanner.next();
}
}
break;
}
}
Car car = new Car(name, speed);
System.out.println("Самая быстрая машина: " + race.returnLeader(car).name);
}
scanner.close();
}


}
13 changes: 13 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class Race {
int distance = 0;
Car theFirstCar;
public Car returnLeader(Car car) {

if ((24 * car.speed) > distance) {
distance = 24 * car.speed;
theFirstCar = car;
}
return theFirstCar;
}
}