-
Notifications
You must be signed in to change notification settings - Fork 0
Мой коммит ПР№1. #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
base: main
Are you sure you want to change the base?
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 +1,3 @@ | ||
| # Пустой репозиторий для работы с Java кодом в Android Studio | ||
| 1я практическая работа. Черников Д.В. | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| public 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; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,44 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| Race race = new Race(); // создаем объект класса Race | ||
|
|
||
| for (int i = 1; i <=3; i++) { // цикл для ввода данных для 3 машин | ||
|
|
||
| // введение названия | ||
| System.out.println("Введите название машины №" + i + ":"); | ||
| String name = scanner.nextLine(); // считываем название | ||
|
|
||
| while(name.isEmpty()) { // проверяем, вдруг название пусто | ||
|
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. Код для считывания непустой строки с ввода лучше вынести в отдельную функцию - код, разделённый на небольшие функции, легче читать, поддерживать и переиспользовать |
||
| System.out.println("Название отсутствует. Введите название машины:"); | ||
| name = scanner.nextLine(); | ||
| } | ||
|
|
||
| // введение скорости | ||
| int speed; | ||
| while (true) { // цикл для ввода корректной скорости | ||
|
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. Не рекомендую писать бесконечные циклы через |
||
| System.out.println("Введите скорость машины №" + i + ":"); | ||
|
|
||
| if (scanner.hasNextInt()) { // проверяем, что будет введено целое число | ||
| speed = scanner.nextInt(); | ||
| scanner.nextLine(); //очистка буфера | ||
|
|
||
| if (speed > 0 && speed <= 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("Скорость должна быть в пределах 0 - 250"); | ||
| } | ||
| } else { | ||
| System.out.println("Необходимо ввести число"); // если не число | ||
| scanner.nextInt(); // очситка буфера в случает неверного ввода | ||
| } | ||
| } | ||
|
|
||
| Car car = new Car(name, speed); // создаем объект класса Car | ||
| race.checkLeader(car); // проверяем, является ли эта машина лидером | ||
| } | ||
| System.out.println("Самая быстрая машина: " + race.getLeaderName()); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| public class Race { | ||
| private String leaderName = ""; | ||
| private int leaderDistance = 0; | ||
|
|
||
| public void checkLeader(Car car) { | ||
| int distance = car.getSpeed() * 24; | ||
|
|
||
| if (distance > leaderDistance) { | ||
| leaderDistance = distance; | ||
| leaderName = car.getName(); | ||
| } | ||
| } | ||
|
|
||
| public String getLeaderName() { | ||
| return leaderName; | ||
| } | ||
| } |
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, тем самым исключив возможность их модификации извне. Также их можно сделать публичными и убрать геттеры