Repository navigation
ДЗ 2-го спринта #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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| public class Car { | ||
| String name; | ||
| int speed; | ||
|
|
||
| Car(String name, int speed) { | ||
| this.name = name; | ||
| this.speed = speed; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class CheckInput { | ||
| Scanner scanner = new Scanner(System.in); | ||
| String carName; | ||
| int carSpeed; | ||
| String cancelInput = "Неправильный ввод. Попробуйте еще раз"; | ||
|
|
||
| String checkInputName() { | ||
|
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. Здесь |
||
| while (true) { | ||
| if (!(scanner.hasNextInt())) { | ||
| carName = scanner.next(); | ||
| if (carName.trim().isEmpty()) { | ||
| System.out.println("Вы ничего не ввели."); | ||
| } else return carName; | ||
| } else { | ||
| System.out.println(cancelInput); | ||
| } | ||
| scanner.next(); | ||
| } | ||
| } | ||
|
|
||
| int checkInputSpeed() { | ||
|
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. С этой функцией аналогично комментарию выше |
||
| while (true) { | ||
| if (scanner.hasNextInt()) { | ||
| carSpeed = scanner.nextInt(); | ||
| if (carSpeed < 0 || carSpeed > 250) { | ||
| System.out.println("Введите число, которое больше 0, но меньше или равно 250."); | ||
| } else return carSpeed; | ||
| } else { | ||
| System.out.println(cancelInput); | ||
| } | ||
| scanner.next(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,26 @@ | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Car[] cars; | ||
| int number; | ||
| String carName; | ||
| int carSpeed; | ||
| Race race; | ||
|
|
||
| System.out.println("Давайте отправимся на автогонку \"24 часа Ле-Мана\" - и понаблюдаем за исходом событий!"); | ||
| cars = new Car[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. В программе можно избавиться от хранения массива и лишней итерации цикла по массиву машин, определяя победителя и сохраняя только его при вводе значений, таким образом программа будет работать быстрее и требовать меньше памяти |
||
| for (int i = 0; i < 3; i++) { | ||
| number = i + 1; | ||
| System.out.println("Введите название " + number + "-й машины."); | ||
| CheckInput input = new CheckInput(); | ||
| carName = input.checkInputName(); | ||
| System.out.println("Введите скорость " + number + "-й машины."); | ||
| carSpeed = input.checkInputSpeed(); | ||
| cars[i] = new Car(carName, carSpeed); | ||
| System.out.println("Вы создали автомобиль " + carName + ", который едет со скоростью " + carSpeed + " км/ч"); | ||
| } | ||
| race = new Race(); | ||
| race.calculate(cars); | ||
| System.out.println("Самая быстрая машина: " + race.nameOfWinner); | ||
| System.out.println("За 24 часа она проехала " + race.bestDistance); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| public class Race { | ||
| int bestDistance; | ||
| String nameOfWinner; | ||
| int distance; | ||
|
Comment on lines
+2
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. Здесь эти переменные лучше спрятать за модификатором видимости
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. Большое спасибо за подробный фидбек! |
||
|
|
||
| void calculate(Car[] cars) { | ||
| for (int i = 0; i < cars.length; i++) { | ||
| distance = cars[i].speed * 24; | ||
| if (i == 0) { | ||
| bestDistance = distance; | ||
| nameOfWinner = cars[i].name; | ||
| } else if (distance > bestDistance) { | ||
| bestDistance = cars[i].speed * 24; | ||
| nameOfWinner = cars[i].name; | ||
| } else return; | ||
| } | ||
| } | ||
| } | ||
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, тем самым исключив возможность их модификации извне