-
Notifications
You must be signed in to change notification settings - Fork 0
Создание консольного приложения #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?
Conversation
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| CarRace carRaces = new CarRace(); | ||
| carRaces.start(); | ||
| carRaces.displayWinner(); |
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.
Нет особого смысла выносить абсолютно всё из main. В твоём случае CarRace это God Object.
|
|
||
| class CarRace { | ||
| Scanner scanner = new Scanner(System.in); | ||
| ArrayList <Car> carList = 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.
ArrayList это имплементация интерфейса List. Правильнее будет объявить так:
List<Car> carList = new ArrayList()
| for (int i=1;i<=3;i++){ | ||
| System.out.println("Введите название автомобиля номер " +i +":"); | ||
| String nameCar = scanner.nextLine(); | ||
| int speedCar; | ||
| while (true){ | ||
| System.out.println("Введите скорость (от 0 до 250) автомобиля номер " +i +":"); | ||
| speedCar = Integer.parseInt(scanner.nextLine()); | ||
| if (speedCar >= 0 && speedCar <= 250) { | ||
| break; | ||
| } else { | ||
| System.out.println("Скорость должна быть в диапазоне от 0 до 250. Попробуйте снова."); | ||
|
|
||
| }} | ||
|
|
||
| carList.add(new Car(nameCar, speedCar)); | ||
| distance(nameCar, speedCar); | ||
| } | ||
|
|
||
| scanner.close(); | ||
|
|
||
| } | ||
| } |
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.
Всё очень грустно с индентацией(пробелами в начале строк). Тяжело читать такой код, нужно поправить.
| String name; | ||
| int speed; |
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.
пока не проходили это, но изучу
… при вводе букв или не целого числа, выводится сообщение об ошибке и просит новый ввод.
Создание консольного приложения для поиска победителя гонок после ввода данных.