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

Добавлены файлы классов Race и Car, добавлен код в классе Main #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
anton8355 wants to merge 1 commit into main
base: main
Choose a base branch
Loading
from dev
Open
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions src/main/java/Car.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class Car {
String name;
int speed;

Car(String name, int speed) {
this.name = name;
this.speed = speed;
}

public String getName() {
return name;
}

public int calculateDistance() {
return speed * 24;
}

}
65 changes: 63 additions & 2 deletions src/main/java/Main.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,67 @@
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Set;

public class Main {

final static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
System.out.println("Hello world!");
Car[] cars = new Car[3];
Set<String> usedNames = new HashSet<>(); //Здесь будет храниться список введенных названий машин
for (int i = 0; i < 3; i++) {
String carName;
int carSpeed;

while (true) {
System.out.printf("Введите название машины No%d: ", i + 1);
carName = inputCarName(i);
if (!usedNames.contains(carName)) {
usedNames.add(carName);
break;
} else {
System.out.printf("Машина %s уже существует. Пожалуйста, введите другое название.\n", carName);
}
}
carSpeed = inputCarSpeed(i);
cars[i] = new Car(carName, carSpeed);
}

Race race = new Race(cars);
Car leader = race.findLeader();

System.out.printf("Самая быстрая машина: %s", leader.getName());

}

public static String inputCarName(int i) {
while (true) {
String name = scanner.nextLine().trim();
if (!name.isEmpty()) {
return name;
} else {
System.out.println("Введено пустое название. Пожалуйста, введите название автомобиля.");
System.out.printf("Введите название машины No%d: ", i + 1);
}
}
}

public static int inputCarSpeed(int i) {
while (true) {
System.out.printf("Введите скорость машины No%d: ", i + 1);
try {
int speed = scanner.nextInt();
scanner.nextLine();
if (speed > 0 && speed <= 250) {
return speed;
} else {
System.out.println("Неправильная скорость");
}
} catch (InputMismatchException e) {
System.out.println("Ошибка: введено не число");
scanner.nextLine();
}
}
}
}
}
17 changes: 17 additions & 0 deletions src/main/java/Race.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class Race {
private final Car[] cars;

Race(Car[] cars) {
this.cars = cars;
}

public Car findLeader() {
Car leader = cars[0];
for (Car car : cars) {
if (car.calculateDistance() > leader.calculateDistance()) {
leader = car;
}
}
return leader;
}
}

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