Repository navigation
First #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?
First #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,42 @@ | ||
| import java.util.InputMismatchException; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Car { | ||
| String name; | ||
| int speed; | ||
|
|
||
| /*Перегружаю конструктор класса, чтобы была возможность использовать условно пустой экземпляр | ||
| автомобиля, который нужен при сравнении автомобилей. | ||
| */ | ||
| public Car(){ | ||
| this.name = ""; | ||
| this.speed = 0; | ||
| } | ||
| // логика создания нового автомобиля из конструктора | ||
| public Car(int num){ | ||
| Scanner scanner = new Scanner(System.in); | ||
| System.out.println("Введите название автомобиля №" + num +":"); | ||
| name = scanner.next(); | ||
| setSpeed(num); | ||
| } | ||
| /* логика установки скорости с валидацией введенных симоволов и рекурсивно заставляю ввести | ||
| верное значение с указанием какие значения от него ожидаются | ||
| */ | ||
| public void setSpeed(int num){ | ||
| Scanner scanner = new Scanner(System.in); | ||
| System.out.println("Введите скорость автомобиля №" + num + ":"); | ||
| int curentSpeed; | ||
| try { | ||
| curentSpeed = scanner.nextInt(); | ||
| if (curentSpeed > 0 && curentSpeed <=250){ | ||
| speed=curentSpeed; | ||
| }else{ | ||
| System.out.println("Введенное значение находится вне пределов от 1 до 250 включительно. Попробуйте снова."); | ||
| setSpeed(num); | ||
| } | ||
| } catch (InputMismatchException e){ | ||
| System.out.println("Необходимо ввести число от 1 до 250 включительно."); | ||
| setSpeed(num); | ||
| } | ||
|
Comment on lines
+29
to
+40
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. try-catch довольно дорогая операция, правильнее было бы использовать |
||
| } | ||
|
Comment on lines
+26
to
+41
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. Логичнее было бы хранить num внутри самого класса Car как переменную и задавать в конструкторе. 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 и оставить тут только простой геттер. |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Race race = new Race(); | ||
| race.addCars(3); | ||
| race.whoWinn(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public class Race { | ||
| //Переменная хранит участников гонки | ||
| ArrayList<Car> arrCar = new ArrayList<>(); | ||
|
|
||
| //Добавляем участников гонки, предусматриваем возможность изменения количества участников | ||
| void addCars(int i){ | ||
| for (int j=0; j<i; j++){ | ||
| Car car = new Car(j+1); | ||
| arrCar.add(car); | ||
| } | ||
| } | ||
| //Вычисляем победителя | ||
| void whoWinn() { | ||
| Car car = new Car(); | ||
|
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 distance = 0; | ||
| for (Car e : arrCar) { | ||
| if (distance < e.speed*24) { | ||
| distance = e.speed*24; | ||
| car = e; | ||
| } | ||
| } | ||
| System.out.println("Самая быстрая машина: " + car.name); | ||
| } | ||
| } | ||
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.
В целом по коду уже понятно что делает функция. Здесь не нужен комментарий.
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.
Так же комментарии принято писать на английском, но это, конечно, не обязтательно.