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
AleksandrPolistsuk wants to merge 3 commits 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
68 changes: 65 additions & 3 deletions src/main/java/Main.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,68 @@

import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
CarRace carRaces = new CarRace();
carRaces.start();
carRaces.displayWinner();
Comment on lines 4 to +7
Copy link

Choose a reason for hiding this comment

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

Нет особого смысла выносить абсолютно всё из main. В твоём случае CarRace это God Object.

}
}
class CarRace {
Scanner scanner = new Scanner(System.in);
ArrayList <Car> carList = new ArrayList<>();
Copy link

Choose a reason for hiding this comment

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

ArrayList это имплементация интерфейса List. Правильнее будет объявить так:

List<Car> carList = new ArrayList()

String leadingCar = ""; // строка для победителя
int maxDistance = 0; // строка для пройденой дистанции
// Метод расчета дистанции и сохранения лучших данных
public void distance(String nameCar, int speedCar) {
int distance = speedCar * 24;
if (distance > maxDistance) {
leadingCar = nameCar;
maxDistance = distance;
}
}
// Метод объявлния победителя, если поле не пустое
public void displayWinner() {
if (!leadingCar.isEmpty()) {
System.out.println("Наш победитель " + leadingCar + " проехал дистанцию " + maxDistance + " км");
} else {
System.out.println("Нет данных о победителе.");
}
}
// метод сбора данных и передачи их
public void start () {
System.out.println("Добро пожаловать на гонку 24 часа Ле-Мана!");
System.out.println("Для начала гонки, введите название и скорость 3 автомобилей.");
for (int i=1;i<=3;i++){
System.out.println("Введите название автомобиля номер " +i +":");
String nameCar = scanner.nextLine();
int speedCar;
while (true) {
System.out.println("Введите скорость (от 0 до 250) автомобиля номер " + i + ":");
String input = scanner.nextLine(); // считываем ввод как строку

try {
speedCar = Integer.parseInt(input);

if (speedCar >= 0 && speedCar <= 250) {
break;
} else {
System.out.println("Скорость должна быть в диапазоне от 0 до 250. Попробуйте снова.");
}
} catch (NumberFormatException e) {
System.out.println("Скорость введена некорректно. Пожалуйста, введите целое число от 0 до 250.");
}
}
carList.add(new Car(nameCar, speedCar));
distance(nameCar, speedCar);
}
}
scanner.close();
}
}
// обьект Car, который имеет название и скорость.
class Car {
String name;
int speed;
Comment on lines +63 to +64
Copy link

Choose a reason for hiding this comment

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

В Джаве принято делать поля приватным и писать для них геттеры и сеттеры.

Copy link
Owner Author

@AleksandrPolistsuk AleksandrPolistsuk Sep 19, 2024

Choose a reason for hiding this comment

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

пока не проходили это, но изучу

public Car(String name, int speed) {
this.name = name;
this.speed = speed;
}}

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