-
Notifications
You must be signed in to change notification settings - Fork 0
Initial 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?
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,15 @@ | ||
| public class Auto { | ||
| String name; | ||
| int speed; | ||
| public Auto(String name, int speed) { | ||
| this.name = name; | ||
| this.speed = speed; | ||
| } | ||
| public String getName() { | ||
| return name; | ||
| } | ||
| public int getSpeed() { | ||
| return speed; | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class InputRacerData { | ||
| public static Auto inputRacerData(Scanner scanner) { | ||
| String inputName; | ||
|
|
||
| while (true) { | ||
| System.out.println("Введите имя гонщика: "); | ||
| inputName = scanner.nextLine(); | ||
|
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. Лучше тут ещё воспользоваться функцией |
||
|
|
||
| if (!inputName.isEmpty()) { | ||
| break; | ||
| } else { | ||
| System.out.println("Введите непустое имя"); | ||
| } | ||
| } | ||
cherrevkoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| int inputSpeed; | ||
|
|
||
| while (true) { | ||
| System.out.println("Введите скорость гонщика: "); | ||
|
|
||
| if (scanner.hasNextInt()) { | ||
| inputSpeed = scanner.nextInt(); | ||
| scanner.nextLine(); | ||
|
|
||
| if (inputSpeed > 0 && inputSpeed < 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("Введите корректное значение"); | ||
| scanner.nextLine(); | ||
| } | ||
| } | ||
| return new Auto(inputName, inputSpeed); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,15 @@ | ||
| 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<Auto> autos = 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. От хранения массива машин и лишнего цикла при определении победителя можно избавиться, если при вводе данных сразу вычислять победителя и хранить его в отдельной переменной, тогда программа будет требовать меньше памяти и работать быстрее |
||
|
|
||
| for (int i = 0; i < 3; i++) { | ||
| autos.add(InputRacerData.inputRacerData(scanner)); | ||
| } | ||
| Race race = new Race(); | ||
| race.findLeader(autos); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public class Race { | ||
| String leader = ""; | ||
| int distance = 0; | ||
| int time = 24; | ||
|
|
||
| public void findLeader(ArrayList<Auto> autos) { | ||
|
|
||
| for (Auto auto : autos) { | ||
| int distanceOfRacer = time * auto.getSpeed(); | ||
|
|
||
| if (distanceOfRacer > distance) { | ||
| distance = distanceOfRacer; | ||
| leader = auto.getName(); | ||
| } | ||
| } | ||
| System.out.printf("Лидер гонки - %s, дистанция - %d км", leader, distance ); | ||
| } | ||
|
|
||
| } |
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, тем самым исключив возможность их модификации извне. Тогда можно удалить геттеры и обращаться к полям напрямую