Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

пул реквест для первого проекта в яндекс практикуме #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
posleflamingo wants to merge 1 commit into main
base: main
Choose a base branch
Loading
from dev
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
59 changes: 57 additions & 2 deletions src/main/java/Main.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,61 @@
import java.util.Scanner;
class Car {
private String name;
private int speed;
public Car(String name, int speed) {
this.name = name;
this.speed = speed;
}
public String getName() {
return name;
}
public int getSpeed() {
return speed;
}
}
Comment on lines +2 to +15
Copy link

@ArturNurtdinov ArturNurtdinov May 17, 2025

Choose a reason for hiding this comment

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

Данный класс предназначен только для хранения данных, которые все инициализируется в конструкторе, поэтому можно полям поставить модификатор final, убрать private и геттеры - получится то же самое, но будет прямой доступ к полям и меньше кода

class Race {
private String leaderName = "";
private int maxDistance = 0;

public void checkLeader(Car car) {
int distance = car.getSpeed() * 24;
Copy link

@ArturNurtdinov ArturNurtdinov May 17, 2025

Choose a reason for hiding this comment

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

Для повышения читабельности кода число 24 лучше вынести в константу с говорящим названием

if (distance > maxDistance) {
maxDistance = distance;
leaderName = car.getName();
}
}
public String getLeaderName() {
return leaderName;
}
}
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++) {
System.out.println("Введите название машины No" + i + ":");
String name = scanner.next();

int speed;
while (true) {
System.out.println("Введите скорость машины No" + i + ":");
if (scanner.hasNextInt()) {
speed = scanner.nextInt();
if (speed > 0 && speed <= 250) {
Copy link

@ArturNurtdinov ArturNurtdinov May 17, 2025

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("Неправильная скорость. Введите число от 1 до 250.");
}
} else {
System.out.println("Ошибка ввода. Введите целое число.");
scanner.next();
}
}

Car car = new Car(name, speed);
race.checkLeader(car);
}
System.out.println("Самая быстрая машина: " + race.getLeaderName());
scanner.close();
}
}
}

AltStyle によって変換されたページ (->オリジナル) /