Repository navigation
R4UBS #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?
R4UBS #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,14 @@ | ||
| class Car { | ||
| private String name; | ||
| private int speed; | ||
| public Car(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 |
|---|---|---|
| @@ -1,6 +1,43 @@ | ||
| import java.util.InputMismatchException; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| {System.out.println("Гонка! 24 часа Ле-Мана");} | ||
| Scanner scanner= new Scanner(System.in); | ||
| Race race = new Race(); | ||
|
|
||
| for (int i = 0; i < 3; i++) { | ||
| String carName = readCarName(scanner); | ||
| int carSpeed = readCarSpeed(scanner); | ||
|
|
||
| Car car; | ||
| car = new Car(carName, carSpeed); | ||
| race.updateLeader(car); | ||
| } | ||
| System.out.println("Самая быстрая машина : " + race.getCurrentLeader().getName()); | ||
| scanner.close(); | ||
| } private static String readCarName(Scanner scanner) { | ||
|
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. Объявление функции лучше перенести на новую строку для повышения читабельности кода |
||
| System.out.println("Введите название автомобиля:"); | ||
| return scanner.nextLine(); | ||
| } | ||
| private static int readCarSpeed(Scanner scanner) { | ||
| int carSpeed; | ||
| while (true) { | ||
|
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. Не рекомендую писать бесконечные циклы через |
||
| System.out.println("Введите скорость автомобиля (от 0 до 250 км/ч):"); | ||
| try { | ||
| carSpeed = scanner.nextInt(); | ||
| if (carSpeed >= 0 && carSpeed <= 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. Минимальную и максимальную скорости лучше вынести в константы для повышения читабельности кода |
||
| scanner.nextLine(); | ||
| break; | ||
| } else { | ||
| System.out.println("Скорость должна быть (от 0 до 250 км/ч.):"); | ||
| } | ||
| } catch (InputMismatchException e) { | ||
| System.out.println("Введите целое числовое значение."); | ||
| scanner.nextLine(); // Очистить буфер сканера | ||
| } | ||
| } | ||
| return carSpeed; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| class Race { | ||
| private Car currentLeader= null; | ||
| private int currentLeaderDistance= 0; | ||
| public void updateLeader(Car car) { | ||
| int newCarDistance = 24 * car.getSpeed(); | ||
|
|
||
| if (newCarDistance > currentLeaderDistance) { | ||
| currentLeader= car; | ||
| currentLeaderDistance= newCarDistance; | ||
| } | ||
| } | ||
| public Car getCurrentLeader() { | ||
| return currentLeader; | ||
| } | ||
| } |
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- тогда можно удалить геттеры и получать доступ к полям напрямую по их имени - будет меньше кода, но также надёжно как с геттерами