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: dev
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,14 @@ | ||
| public class Car { | ||
| private final String name; | ||
| private final int spead; | ||
| public Car(String name, int spead) { | ||
| this.name = name; | ||
| this.spead = spead; | ||
| } | ||
| public String getName() { | ||
| return name; | ||
| } | ||
| public int getSpeed (){ | ||
| return spead; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,37 @@ | ||
| 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(); | ||
|
|
||
| for (int i = 1; i <= 3; i++) { | ||
| System.out.println("Введите название машины №" + i + ":"); | ||
| String name = scanner.nextLine(); | ||
|
|
||
| int spead; | ||
| while (true) { | ||
|
|
||
| System.out.println("Введите скорость машины №" + i + ":"); | ||
| if (scanner.hasNextInt()) { | ||
| spead = scanner.nextInt(); | ||
| scanner.nextLine(); | ||
|
|
||
| if (spead > 0 && spead <= 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("Неправельная скорость."); | ||
| } | ||
| } else { | ||
| System.out.println("Пожалуйста, введите число."); | ||
| scanner.nextLine(); | ||
| } | ||
| } | ||
| Car car = new Car(name, spead); | ||
| race.updateLeader(car); | ||
| } | ||
| System.out.println("Самая быстрая машина: " + race.getLeaderName()); | ||
| scanner.close(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| public class Race { | ||
| private String leaderName = ""; | ||
| private int maxDistance = 0; | ||
|
|
||
| public void updateLeader(Car car) { | ||
| int distance = (24) * car.getSpeed(); | ||
|
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 (distance > maxDistance) { | ||
| maxDistance = distance; | ||
| leaderName = car.getName(); | ||
| } | ||
| } | ||
| public String getLeaderName() { | ||
| 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.
Поля можно сделать публичными и удалить функции-геттеры. Т.к. ты пометил поля
final, то можно оставить прямой доступ до полей - это будет так же надёжно, как и с геттерами