-
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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| public class Car { | ||
| String name; | ||
| int speed; | ||
|
Comment on lines
+2
to
+3
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. Поля лучше пометить |
||
|
|
||
| Car(String name, int speed){ | ||
| this.name = name; | ||
| this.speed = speed; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,35 @@ | ||
| import java.util.ArrayList; | ||
| 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 = 0; i<3;i++){ | ||
| System.out.println(String.format("Введите название автомобиля No%d", i+1)); | ||
| String name = scanner.next(); | ||
| int speed; | ||
| while (true) { | ||
| System.out.println(String.format("Введите скорость автомобиля No%d", i + 1)); | ||
| if (scanner.hasNextInt()){ | ||
| speed = scanner.nextInt(); | ||
| 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("Неправильная скорость, должно быть число"); | ||
| } | ||
|
|
||
| } | ||
| Car car = new Car(name, speed); | ||
| race.leader_cheak(car); | ||
|
|
||
| } | ||
| System.out.println(String.format("Самая быстрая машина: %s", race.leader.name)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| public class Race { | ||
| Car leader = new Car("", 0); | ||
| int leader_distance; | ||
|
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. Переменные в Java принято именовать в соответствии с camelCase, лучше переименовать данную переменную
Comment on lines
+2
to
+3
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. Оба поля лучше сделать приватными, а для получения машины-победителя в конце программы написать отдельную функцию-геттер, чтобы данные поля не были доступны всей программе |
||
|
|
||
| void leader_cheak(Car pretendent){ | ||
| if (pretendent.speed > this.leader.speed){ | ||
| this.leader = pretendent; | ||
| this.leader_distance = this.leader.speed*24; | ||
| } | ||
| } | ||
| } | ||