Repository navigation
Домашняя работа #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
kimdeun
wants to merge
1
commit into
main
Choose a base branch
from
dev
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,112 @@ | ||
| 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; | ||
| } | ||
|
|
||
| public int calculateDistance() { | ||
| return 24 * speed; | ||
| } | ||
| } | ||
|
|
||
| // Класс Гонка | ||
| class Race { | ||
| private String leaderName; | ||
| private int leaderDistance; | ||
|
|
||
| public Race() { | ||
| this.leaderName = ""; | ||
| this.leaderDistance = 0; | ||
| } | ||
|
|
||
| public void determineLeader(Car car) { | ||
| int carDistance = car.calculateDistance(); | ||
| if (carDistance > leaderDistance) { | ||
| leaderName = car.getName(); | ||
| leaderDistance = carDistance; | ||
| } | ||
| } | ||
|
|
||
| public String getLeader() { | ||
| return leaderName; | ||
| } | ||
| } | ||
|
|
||
| // Главный класс | ||
| public class Main { | ||
| private static Scanner scanner = new Scanner(System.in); | ||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Race race = new Race(); | ||
|
|
||
| for (int i = 1; i <= 3; i++) { | ||
| System.out.println("--- Автомобиль №" + i + " ---"); | ||
|
|
||
| // Ввод названия автомобиля | ||
| String carName = inputCarName(i); | ||
|
|
||
| // Ввод скорости | ||
| int carSpeed = inputCarSpeed(i); | ||
|
|
||
| // Создание автомобиля и определение лидера | ||
| Car car = new Car(carName, carSpeed); | ||
| race.determineLeader(car); | ||
|
|
||
| System.out.println(); // строка для читаемости | ||
| } | ||
|
|
||
| // Вывод результата | ||
| String winner = race.getLeader(); | ||
| System.out.println("Самая быстрая машина: " + winner); | ||
|
|
||
| scanner.close(); | ||
| } | ||
|
|
||
| // Метод для ввода названия автомобиля | ||
| private static String inputCarName(int carNumber) { | ||
| String carName = ""; | ||
| while (carName.isEmpty()) { | ||
| System.out.print("Введите название машины №" + carNumber + ": "); | ||
| carName = scanner.nextLine().trim(); | ||
| if (carName.isEmpty()) { | ||
| System.out.println("Название машины не может быть пустым!"); | ||
| } | ||
| } | ||
| return carName; | ||
| } | ||
|
|
||
| // Метод для ввода скорости автомобиля | ||
| private static int inputCarSpeed(int carNumber) { | ||
| int carSpeed = 0; | ||
| while (true) { | ||
| System.out.print("Введите скорость машины №" + carNumber + " (1-250 км/ч): "); | ||
| if (scanner.hasNextInt()) { | ||
| carSpeed = scanner.nextInt(); | ||
| scanner.nextLine(); // очистка буфера | ||
| if (carSpeed > 0 && carSpeed <= 250) { | ||
|
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. Минимальную и максимальную скорости лучше вынести в константы для повышения читабельности кода |
||
| break; | ||
| } else { | ||
| System.out.println("Неправильная скорость! Скорость должна быть от 1 до 250 км/ч."); | ||
| } | ||
| } else { | ||
| System.out.println("Ошибка! Введите целое число."); | ||
| scanner.nextLine(); // очистка буфера при ошибке | ||
| } | ||
| } | ||
| return carSpeed; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Поля лучше пометить
final, тем самым исключив возможность их модификации извне. Тогда можно будет удалить геттеры и напрямую обращаться к полям - это будет также надёжно и безопасно, как с геттерами