Repository navigation
Практическая №1 #3
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,11 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <module type="JAVA_MODULE" version="4"> | ||
| <component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
| <exclude-output /> | ||
| <content url="file://$MODULE_DIR$"> | ||
| <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> | ||
| </content> | ||
| <orderEntry type="inheritedJdk" /> | ||
| <orderEntry type="sourceFolder" forTests="false" /> | ||
| </component> | ||
| </module> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # Практическая №1. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| public class Car { | ||
| String name; | ||
| int speed; | ||
| Car(String name, int speed){ | ||
| this.name = name; | ||
| this.speed = speed; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import java.util.Scanner; | ||
| public class Main { | ||
| public static void main(String[] args) { | ||
|
|
||
| int numberCars=3; | ||
|
|
||
| Car[] cars = new Car[numberCars]; | ||
|
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. От хранения массива машин и лишнего цикла при определении победителя можно избавиться, если при вводе данных сразу вычислять победителя и хранить его в отдельной переменной, тогда программа будет требовать меньше памяти и работать быстрее |
||
|
|
||
| for (int i = 0; i < cars.length; i++) { | ||
| System.out.println("Введите название машины №" + (i + 1) + ":"); | ||
| String nameCar = checkCarName(); | ||
| System.out.println("Введите скорость машины №" + (i + 1) + ":"); | ||
| int speedCar = checkCarSpeed(); | ||
| cars[i] = new Car(nameCar, speedCar); | ||
| } | ||
|
|
||
| Race.leManRace(cars); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| static String checkCarName() { | ||
| Scanner scanner = new Scanner(System.in); | ||
|
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. Сканнер лучше не создавать на каждое чтение, т.к. при создании сканнеры открывается новый входной поток, поэтому лучше 1 раз создать и переиспользовать |
||
| String name; | ||
| while (true) { | ||
| name = scanner.nextLine(); | ||
| if (!(name.trim().isEmpty())) { | ||
| return name; | ||
| } else { | ||
| System.out.println("Пустая строка!"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public static int checkCarSpeed() { | ||
| Scanner scanner = new Scanner(System.in); | ||
| while (true) { | ||
| if (scanner.hasNextInt()) { | ||
| int speed = scanner.nextInt(); | ||
| if ((0 < speed) && (speed <= 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. Минимальную и максимальную скорости лучше вынести в константы для повышения читабельности кода |
||
| return speed; | ||
|
|
||
| } else { | ||
| System.out.println("Неправильная скорость!"); | ||
| } | ||
| } else { | ||
| System.out.println("Введите целое число:"); | ||
| scanner.next(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| public class Race { | ||
|
|
||
| static void leManRace(Car[] cars){ | ||
| int hours = 24; | ||
| String fastCar =""; | ||
| for (int i=1; i<cars.length; i++) { | ||
| if (calculateDistance(cars[i].speed, hours) > calculateDistance(cars[i - 1].speed, hours)) { | ||
| fastCar = cars[i].name; | ||
| } else { | ||
| fastCar = cars[i - 1].name; | ||
| } | ||
| } | ||
| System.out.println("Самая быстрая машина: "+fastCar); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| static int calculateDistance(int speed,int hours){ | ||
| return speed * hours; | ||
| } | ||
| } | ||
|
|
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, тем самым исключив возможность их модификации извне