forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Результат Проектной работы № 1 (Спринт № 2) #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
Open
ikos13
wants to merge
1
commit into
main
Choose a base branch
from
dev_ikos13
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,145 @@ | ||
|
|
||
| package org.example; | ||
| import java.util.Scanner; | ||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
|
|
||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| Rase calculateLider = new Rase(); | ||
|
|
||
| Automobil moskvich = new Automobil(null, 0); | ||
| while (true) { | ||
| System.out.println("Введите название машины №1:"); | ||
| moskvich.name = scanner.nextLine(); //без Line не реагирует на пустой ввод - точнее не завершает его | ||
| if (moskvich.name.isEmpty()) { | ||
| System.out.println("Название не должно быть пустым!"); | ||
| continue; // иначе break далее завершит цикл и программа перейдет к следующему блоку | ||
| } break; | ||
| } | ||
| while (true) { | ||
| System.out.println("Введите скорость машины №1:"); | ||
|
|
||
| String notEnter = scanner.nextLine(); //для проверки пустого ввода значения скорости + (ниже с преобразователем) если значение не числовое | ||
| if (notEnter.isEmpty()) { | ||
| System.out.println("Значение скорости не должно быть пустым!"); | ||
| continue; | ||
| } | ||
| try { | ||
| moskvich.speed = Integer.parseInt(notEnter); // преобразуется только если было введено интовое значение (поэтому hasNextInt доп-но не нужен) | ||
| } catch (NumberFormatException e) { // вид ошибки преобразования, если строкове значение оказалось не интовое | ||
| System.out.println("Некорректное значение скорости! Введите целое число!"); | ||
| continue; | ||
| } | ||
| if (moskvich.speed < 0 || moskvich.speed > 250) { // вместо вложенного цикла объединил проверку по ним в одном | ||
| System.out.println("Скорость должна быть в диапазоне от 0 до 250!"); | ||
| continue; | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| calculateLider.newLider(moskvich.name, moskvich.speed); | ||
|
|
||
| Automobil volga = new Automobil(null, 0); | ||
| while (true) { | ||
| System.out.println("Введите название машины №2:"); | ||
| volga.name = scanner.nextLine(); | ||
| if (volga.name.isEmpty()) { | ||
| System.out.println("Название не должно быть пустым!"); | ||
| continue; | ||
| } break; | ||
| } | ||
| while (true) { | ||
| System.out.println("Введите скорость машины №2:"); | ||
|
|
||
| String notEnter = scanner.nextLine(); | ||
| if (notEnter.isEmpty()) { | ||
| System.out.println("Значение скорости не должно быть пустым!"); | ||
| continue; | ||
| } | ||
| try { | ||
| volga.speed = Integer.parseInt(notEnter); | ||
| } catch (NumberFormatException e) { | ||
| System.out.println("Некорректное значение скорости! Введите целое число!"); | ||
| continue; | ||
| } | ||
| if (volga.speed < 0 || volga.speed > 250) { | ||
| System.out.println("Скорость должна быть в диапазоне от 0 до 250!"); | ||
| continue; | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| calculateLider.newLider(volga.name, volga.speed); | ||
|
|
||
| Automobil lada = new Automobil(null, 0); | ||
| while (true) { | ||
| System.out.println("Введите название машины №3:"); | ||
| lada.name = scanner.nextLine(); | ||
| if (lada.name.isEmpty()) { | ||
| System.out.println("Название не должно быть пустым!"); | ||
| continue; | ||
| } break; | ||
| } | ||
| while (true) { | ||
| System.out.println("Введите скорость машины №3:"); | ||
|
|
||
| String notEnter = scanner.nextLine(); | ||
| if (notEnter.isEmpty()) { | ||
| System.out.println("Значение скорости не должно быть пустым!"); | ||
| continue; | ||
| } | ||
| try { | ||
| lada.speed = Integer.parseInt(notEnter); | ||
| } catch (NumberFormatException e) { | ||
| System.out.println("Некорректное значение скорости! Введите целое число!"); | ||
| continue; | ||
| } | ||
| if (lada.speed < 0 || lada.speed > 250) { | ||
| System.out.println("Скорость должна быть в диапазоне от 0 до 250!"); | ||
| continue; | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| calculateLider.newLider(lada.name, lada.speed); | ||
|
|
||
| System.out.println("Самая быстрая машина: " + calculateLider.lider); | ||
|
|
||
| scanner.close(); | ||
| } | ||
| } | ||
|
|
||
| class Rase { | ||
| String lider; | ||
| int oldDistance; | ||
|
|
||
| public Rase() { | ||
| this.lider = ""; | ||
| this.oldDistance = 0; | ||
| } | ||
|
|
||
| void newLider (String name, int speed) { | ||
| int distance = speed*24; | ||
| if (lider.isEmpty()) { | ||
| lider = name; | ||
| oldDistance = distance; | ||
| } else { | ||
| if (distance > oldDistance) { | ||
| lider = name; | ||
| oldDistance = distance; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class Automobil { | ||
|
|
||
| String name; | ||
| int speed; | ||
|
|
||
| public Automobil(String name, int speed) { | ||
| this.name = name; | ||
| this.speed = speed; | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Рекомендация:
можно сократить количество кода, используя циклы и работу со списком - вместо отдельных описаний каждой сущности, можно пройтись в цикле три раза и заполнить тем самым список из трех автомобилей, тогда получится в три раза меньше кода