-
Notifications
You must be signed in to change notification settings - Fork 0
2 #2
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: qw
Are you sure you want to change the base?
2 #2
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 |
|---|---|---|
| @@ -1,6 +1,61 @@ | ||
| 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 < 4; i++) { | ||
| System.out.println("Введите название машины №" + i); | ||
| String carName = scanner.next(); | ||
| int carSpeed = 0; | ||
|
|
||
|
|
||
| while (true) { | ||
| System.out.println("Введите скорость машины " + i); | ||
|
|
||
| if (scanner.hasNextInt()) { | ||
| carSpeed = scanner.nextInt(); | ||
| if (carSpeed > 0 && carSpeed <= 250) { | ||
| Car car = new Car(carName, carSpeed); | ||
| race.Racing(carName, carSpeed); | ||
| break; | ||
| } else { | ||
| System.out.println("Неверная скорость."); | ||
| } | ||
| } else { | ||
| System.out.println("Неверный ввод. Пожалуйста, введите целое число для скорости."); | ||
| scanner.next(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| System.out.println("Самая быстрая машина " + race.Winner()); | ||
| } | ||
| } | ||
|
|
||
| class Car { | ||
| String carName; | ||
| int carSpeed; | ||
|
Comment on lines
+38
to
+39
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. Поля лучше пометить |
||
|
|
||
| public Car(String carName, int carSpeed) { | ||
| this.carName = carName; | ||
| this.carSpeed = carSpeed; | ||
| } | ||
| } | ||
|
|
||
| class Race { | ||
| int distance = 0; | ||
| String carNameq = ""; | ||
|
Comment on lines
+48
to
+49
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. Переменные лучше сделать приватными, чтобы исключить их модификацию извне |
||
|
|
||
| public void Racing(String carName, int carSpeed) { | ||
| if (carSpeed * 24 > distance) { | ||
| carNameq = carName; | ||
| distance = carSpeed * 24; | ||
| } | ||
| } | ||
|
|
||
| public String Winner() { | ||
|
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. Названия функций принято делать глаголами и с маленькой буквы, хорошим неймингом здесь будет |
||
| return carNameq; | ||
| } | ||
| } | ||
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.
Переменные рекомендуется объявлять максимально близко к их первому месту использования для читабельности кода