-
Notifications
You must be signed in to change notification settings - Fork 0
Создание консольного приложения #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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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<>(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ArrayList это имплементация интерфейса List. Правильнее будет объявить так: |
||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В Джаве принято делать поля приватным и писать для них геттеры и сеттеры.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. пока не проходили это, но изучу |
||
| public Car(String name, int speed) { | ||
| this.name = name; | ||
| this.speed = speed; | ||
| }} | ||