-
Notifications
You must be signed in to change notification settings - Fork 0
secon PR (second changes) #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 @@ | ||
| # Пустой репозиторий для работы с Java кодом в Android Studio | ||
| //попытка 2 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,49 @@ | ||
| 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(); | ||
|
|
||
| for (int i = 1; i <= 3; i++) { | ||
| String name; | ||
| int speed; | ||
| while (true) { | ||
| System.out.println("— Введите название машины № " + i + ":"); | ||
| name = scanner.nextLine().trim(); | ||
| if(!name.isEmpty()){ | ||
| break; | ||
| }else{ | ||
| System.out.println("— Название не может быть пустым!"); | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| 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("— Неправильная скорость"); | ||
| } | ||
| }else { | ||
| System.out.println("— Неправильная скорость"); | ||
| scanner.nextLine(); | ||
| } | ||
| } | ||
| autoCar currentCar = new autoCar(name, speed); | ||
|
|
||
| race.checkLeader(currentCar); | ||
| } | ||
| scanner.close(); | ||
|
|
||
| System.out.println("Самая быстрая машина: " + race.getLeader().nameAuto); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| public class Race { | ||
| autoCar leader; | ||
| public void checkLeader(autoCar car){ | ||
| if(leader == null || car.speedAuto > leader.speedAuto ){ | ||
| leader = car; | ||
| } | ||
| } | ||
| public autoCar getLeader(){ | ||
| return leader; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| public class autoCar { | ||
|
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. Классы в джава и котлине принято называть с большой буквы |
||
|
|
||
| String nameAuto; | ||
| int speedAuto; | ||
|
Comment on lines
+3
to
+4
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. Поля лучше пометить |
||
|
|
||
|
|
||
| autoCar(String nameAuto, int speedAuto ){ | ||
| this.nameAuto = nameAuto; | ||
| this.speedAuto = speedAuto; | ||
| } | ||
|
|
||
| } | ||
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.
Код для считывания непустой строки с ввода лучше вынести в отдельную функцию - код, разделённый на небольшие функции, легче читать, поддерживать и переиспользовать