Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Результат Проектной работы No 1 (Спринт No 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
base: main
Choose a base branch
Loading
from dev_ikos13
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 142 additions & 3 deletions src/main/java/Main.java
View file Open in desktop
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);
Copy link

@sukhoikms27 sukhoikms27 Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Рекомендация:
можно сократить количество кода, используя циклы и работу со списком - вместо отдельных описаний каждой сущности, можно пройтись в цикле три раза и заполнить тем самым список из трех автомобилей, тогда получится в три раза меньше кода

ikos13 reacted with thumbs up emoji
while (true) {
System.out.println("Введите название машины No1:");
moskvich.name = scanner.nextLine(); //без Line не реагирует на пустой ввод - точнее не завершает его
if (moskvich.name.isEmpty()) {
System.out.println("Название не должно быть пустым!");
continue; // иначе break далее завершит цикл и программа перейдет к следующему блоку
} break;
}
while (true) {
System.out.println("Введите скорость машины No1:");

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("Введите название машины No2:");
volga.name = scanner.nextLine();
if (volga.name.isEmpty()) {
System.out.println("Название не должно быть пустым!");
continue;
} break;
}
while (true) {
System.out.println("Введите скорость машины No2:");

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("Введите название машины No3:");
lada.name = scanner.nextLine();
if (lada.name.isEmpty()) {
System.out.println("Название не должно быть пустым!");
continue;
} break;
}
while (true) {
System.out.println("Введите скорость машины No3:");

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;
}
}
}

AltStyle によって変換されたページ (->オリジナル) /