-
Notifications
You must be signed in to change notification settings - Fork 0
first laba #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 laba #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,8 @@ | ||
| class Car { | ||
| String nameCar; | ||
| int speedCar; | ||
| public Car(String nameCar, int speedCar) { | ||
| this.nameCar = nameCar; | ||
| this.speedCar = speedCar; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,46 @@ | ||
|
|
||
| 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 number=1;number<=3;number++) { | ||
| String nameCar; | ||
| int speedCar = 0; | ||
| boolean isValid = false; | ||
| while (true) { | ||
| System.out.println("Введите название машины №" + number); | ||
| nameCar = scanner.nextLine(); | ||
| if (nameCar.isEmpty()) { | ||
| System.out.println("Строка пустая, введите заново."); | ||
| } else | ||
| break; | ||
| } | ||
| while (!isValid) { | ||
| System.out.println("Введите скорость машины №" + number); | ||
| String speedCars = scanner.nextLine(); | ||
|
|
||
| if (speedCars.trim().isEmpty()) { | ||
| System.out.println("Ошибка: ввод не может быть пустым!"); | ||
| continue; | ||
| } | ||
|
|
||
| try { | ||
| speedCar = Integer.parseInt(speedCars.trim()); | ||
| if (speedCar < 0 || speedCar > 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. Минимальную и максимальную скорости лучше вынести в константы для повышения читабельности кода |
||
| System.out.println("Введена некорректная скорость, введите от 0 до 250!"); | ||
| } else { | ||
| isValid = true; | ||
| } | ||
| } catch (NumberFormatException e) { | ||
| System.out.println("Ошибка: введено не целое число или буквы!"); | ||
| } | ||
| } | ||
| Car car = new Car(nameCar, speedCar); | ||
|
|
||
| race.race(car.nameCar,car.speedCar); | ||
| } | ||
| System.out.println("Самая бастрая машина: "+ race.liderCar); | ||
| scanner.close(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| class Race { | ||
| String liderCar=" "; | ||
| int liderDistance=0; | ||
|
Comment on lines
+2
to
+3
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. Поля лучше сделать приватными, чтобы поведение данного класса нельзя было изменить извне, а для получения названия машины-победителя написать отдельную функцию-геттер |
||
| void race(String nameCar,int speedCar){ | ||
|
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=speedCar*24; | ||
| if(distance>liderDistance) | ||
| { | ||
| liderDistance=distance; | ||
| liderCar=nameCar; | ||
| } | ||
| } | ||
|
|
||
| } | ||
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, тем самым исключив возможность их модификации извне