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

Auto(String name, int speed) {
this.name = name;
this.speed = speed;
}
}
36 changes: 34 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);

Auto[] autoArray = new Auto[3];
Race race = new Race();

for (int i = 1; i <= 3; i++) {
System.out.println("Введите название машины №" + i);

String name = scanner.next();

int speed = 0;

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);
if (scanner.hasNextInt()) {
speed = scanner.nextInt();
if ((speed > 0) && (speed <= 250)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Минимальную и максимальную скорости лучше вынести вконстанты для повышения читабельности кода

break;
} else {
System.out.println("Неправильная скорость");
}
} else {
System.out.println("Неправильная скорость");
scanner.next();
}
}

autoArray[i - 1] = new Auto(name, speed);
race.calculateDistance(name, speed);
}
System.out.println("Самая быстрая машина: " + race.name);
}
}
}

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 {
String name = "";
int distance = 0;
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.

Эти поля лучше сделать приватными, чтобы снаружи класса нельзя было сломать внутреннюю логику определения победителя


public void calculateDistance(String name, int speed) {
int distance = speed * 24;
if (distance > this.distance) {
this.name = name;
this.distance = distance;
}
}

}