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

Проектная работа No1 спринта No2 #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
sapphire-hydrangea wants to merge 1 commit into dev
base: dev
Choose a base branch
Loading
from sprint2
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
14 changes: 14 additions & 0 deletions src/main/java/Car.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Car {
String name;
int speed;
Comment on lines +2 to +3
Copy link

@ArturNurtdinov ArturNurtdinov Dec 2, 2025

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

int calcDistance(int hour) {
return speed * hour;
}

}
46 changes: 44 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,48 @@
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);
ArrayList<Car> cars = new ArrayList<>();
Copy link

@ArturNurtdinov ArturNurtdinov Dec 2, 2025

Choose a reason for hiding this comment

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

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


for (int i = 0; i < 3; i++) {
System.out.println("Введите название автомобиля No" + (i + 1) + ":");
String carName = scanner.nextLine();
carName = carName.trim();
while (carName.isEmpty()) {
System.out.println("Введена пустая строка. Введите название автомобиля No" + (i + 1) + " повторно:");
carName = scanner.nextLine();
carName = carName.trim();
}
Comment on lines +12 to +17
Copy link

@ArturNurtdinov ArturNurtdinov Dec 2, 2025

Choose a reason for hiding this comment

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

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


System.out.println("Введите скорость автомобиля No" + (i + 1) + ":");
int speed;
Copy link

@ArturNurtdinov ArturNurtdinov Dec 2, 2025

Choose a reason for hiding this comment

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

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

while (true) {
String strSpeed = scanner.nextLine();
strSpeed = strSpeed.trim();
if (strSpeed.isEmpty()) {
System.out.println("Введена пустая строка. Введите скорость автомобиля No" + (i + 1) + " повторно:");
continue;
}
try {
speed = Integer.parseInt(strSpeed);
} catch (NumberFormatException ex) {
System.out.println("Введено не число. Введите скорость автомобиля No" + (i + 1) + " повторно:");
continue;
}
if (speed <= 0 || speed > 250) {
Copy link

@ArturNurtdinov ArturNurtdinov Dec 2, 2025

Choose a reason for hiding this comment

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

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

System.out.println("Скорость вне возможного диапазона (0;250]. Введите скорость автомобиля No" + (i + 1) + " повторно:");
continue;
}
break;
}

Car car = new Car(carName, speed);
cars.add(car);
}

Race race = new Race();
race.calcWinner(cars);
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/Race.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.util.ArrayList;

public class Race {

void calcWinner(ArrayList<Car> cars) {
int hour = 24;
String leader = "";
int winnerDistance = 0;
for (int i = 0; i < cars.size(); i++) {
Car car = cars.get(i);
int distance = car.calcDistance(hour);
if (distance > winnerDistance) {
winnerDistance = distance;
leader = car.name;
}
}
System.out.println("Самая быстрая машина: " + leader);
}

}

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