Repository navigation
Init commit #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?
Init commit #1
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 modelCar; | ||
| int speed; | ||
|
|
||
| public Car(String modelCar, int speed) { | ||
| this.modelCar = modelCar; | ||
| this.speed = speed; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,51 @@ | ||
|
|
||
| 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(); | ||
| ArrayList<Car> cars = new ArrayList<>(); | ||
|
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. От хранения массива машин и лишнего цикла при определении победителя можно избавиться, если при вводе данных сразу вычислять победителя и хранить его в отдельной переменной, тогда программа будет требовать меньше памяти и работать быстрее |
||
| int maxSpeed = 0; | ||
|
|
||
| for (int i = 1; i <= 3; i++) { | ||
|
|
||
| String modelCar; | ||
| int speed; | ||
|
|
||
| System.out.printf("Введите название %d автомобиля:\n", i); | ||
| modelCar = scanner.next(); | ||
|
|
||
| System.out.printf("Введите скорость %d автомобиля:\n", i); | ||
|
|
||
| while (!scanner.hasNextInt()) { | ||
| System.out.println("Ошибка ввода"); | ||
| scanner.next(); | ||
| System.out.printf("Введите скорость %d автомобиля:\n", i); | ||
| } | ||
|
|
||
| speed = scanner.nextInt(); | ||
|
|
||
| while (speed < 0 || speed > 250) { | ||
| System.out.println("Ошибка ввода. Скорость должна быть от 0 до 250 км/ч."); | ||
| System.out.printf("Введите скорость %d автомобиля:\n", i); | ||
| while (!scanner.hasNextInt()) { | ||
| System.out.println("Ошибка ввода"); | ||
| scanner.next(); | ||
| System.out.printf("Введите скорость %d автомобиля:\n", i); | ||
| } | ||
| speed = scanner.nextInt(); | ||
| } | ||
|
|
||
| cars.add(new Car(modelCar, speed)); | ||
| } | ||
| for (Car car : cars) { | ||
| if (car.speed >= maxSpeed) { | ||
| maxSpeed = car.speed; | ||
| race.leader = car.modelCar; | ||
| } | ||
| } | ||
| System.out.println("Лидер: " + race.leader); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public class Race { | ||
|
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. Логику с определением победителя лучше в этот класс перенести из Main, т.к. функциональность определения победителя больше к классу |
||
|
|
||
| String leader = ""; | ||
|
|
||
| ArrayList<Car> cars = new ArrayList<>(); | ||
|
|
||
|
|
||
| } | ||
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, тем самым исключив возможность их модификации извне