Repository navigation
Практическая 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,6 +1,70 @@ | ||
| 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); | ||
| ArrayList<Car> carsList = new ArrayList<>(); | ||
| for (int i = 0; i < 3; i++) { | ||
| System.out.println("Введите название автомобиля:"); | ||
| String name = scanner.next(); | ||
| int speed; | ||
|
|
||
| while (true) { | ||
| System.out.println("Введите скорость автомобиля:"); | ||
|
|
||
| if (scanner.hasNextInt()) { | ||
| speed = scanner.nextInt(); | ||
| if (speed > 0 && speed < 250) { | ||
| break; | ||
| } else { | ||
| System.out.println("Скорость должна быть больше 0 и меньше 250. Введите скорость автомобиля:"); | ||
| } | ||
| } else { | ||
| System.out.println("Скорость может быть только целым числом. Введите скорость автомобиля"); | ||
| scanner.next(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Car car = new Car(speed, name); | ||
| carsList.add(car); | ||
| } | ||
| scanner.close(); | ||
| Race race = new Race(carsList); | ||
| System.out.println("Самая быстрая машина: " + race.calculateLeader()); | ||
| } | ||
| } | ||
|
|
||
| class Car { | ||
| int speed; | ||
|
|
||
| String name; | ||
|
Comment on lines
+40
to
+42
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(int speed, String name) { | ||
| this.speed = speed; | ||
| this.name = name; | ||
| } | ||
| } | ||
|
|
||
| class Race { | ||
| private ArrayList<Car> carsList; | ||
|
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. От хранения массива машин и лишнего цикла при определении победителя можно избавиться, если при вводе данных сразу вычислять победителя и хранить его в отдельной переменной, тогда программа будет требовать меньше памяти и работать быстрее |
||
|
|
||
| Race(ArrayList<Car> carsList) { | ||
| this.carsList = carsList; | ||
| } | ||
|
|
||
| String calculateLeader() { | ||
| int leaderDistance = 0; | ||
| String leaderName = null; | ||
| for (int i = 0; i < carsList.size(); i++) { | ||
| Car car = carsList.get(i); | ||
| int distance = car.speed * 24; | ||
| if (distance > leaderDistance) { | ||
| leaderDistance = distance; | ||
| leaderName = car.name; | ||
| } | ||
| } | ||
| 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.
Минимальную и максимальную скорости лучше вынести в константы для повышения читабельности кода