Skip to content
Open

2 #2

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
57 changes: 56 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,61 @@
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 < 4; i++) {
System.out.println("Введите название машины №" + i);
String carName = scanner.next();
int carSpeed = 0;
Comment on lines +9 to +11

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);

if (scanner.hasNextInt()) {
carSpeed = scanner.nextInt();
if (carSpeed > 0 && carSpeed <= 250) {
Car car = new Car(carName, carSpeed);
race.Racing(carName, carSpeed);
break;
} else {
System.out.println("Неверная скорость.");
}
} else {
System.out.println("Неверный ввод. Пожалуйста, введите целое число для скорости.");
scanner.next();
}
}
}

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

class Car {
String carName;
int carSpeed;
Comment on lines +38 to +39

Choose a reason for hiding this comment

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

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


public Car(String carName, int carSpeed) {
this.carName = carName;
this.carSpeed = carSpeed;
}
}

class Race {
int distance = 0;
String carNameq = "";
Comment on lines +48 to +49

Choose a reason for hiding this comment

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

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


public void Racing(String carName, int carSpeed) {
if (carSpeed * 24 > distance) {
carNameq = carName;
distance = carSpeed * 24;
}
}

public String Winner() {

Choose a reason for hiding this comment

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

Названия функций принято делать глаголами и с маленькой буквы, хорошим неймингом здесь будет getWinner()

return carNameq;
}
}