Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Пустой репозиторий для работы с Java кодом в Android Studio
24 часа Ле-Мана
18 changes: 18 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class Car {
private String name;
private int speed;

Comment on lines +2 to +4
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Переменные можно пометить final и убрать геттеры с модификатором private - тогда можно будет иметь такой же надёжный и безопасный доступ к данным по имени переменной

public Car(String name, int speed) {
this.name = name;
this.speed = speed;
}
public String getName() {
return name;
}
public int getSpeed() {
return speed;
}
public int mileage() {
return 24 * speed;
}
}
48 changes: 44 additions & 4 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
public static void main(String[] args){
System.out.println("24 часа Ле-Мана");
Scanner scanner = new Scanner(System.in);
Car[] cars = new Car[3];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Race race = new Race();

for (int i = 0; i < cars.length; i++) {
System.out.println("Автомобиль " + (i + 1) + ":");

String name;
while (true) {
System.out.println("Введите название автомобиля: ");
name = scanner.nextLine().trim();
if(!name.isEmpty()){
break;
}
System.out.println("Ошибка: название автомобиля не может быть пустым!");
}

int speed = 0;
while (true) {
System.out.println("Введите скорость автомобиля (1-250 км/ч): ");
if (scanner.hasNextInt()) {
speed = scanner.nextInt();
scanner.nextLine();
if (speed > 0 && speed <= 250) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Минимальную и максимальную скорости лучше вынести в константы для повышения читабельности кода

break;
} else {
System.out.println("Скорость должна быть от 1 до 250.");
}
} else {
System.out.println("Введите целое число");
scanner.next();
}
}

cars[i] = new Car(name, speed);
}
}

Car winner = race.raceWinner(cars);
System.out.println("Самая быстрая машина: " + winner.getName());
}
}

15 changes: 15 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class Race {
public Car raceWinner(Car[] cars) {
Car winner = cars[0];
int maxDistance = cars[0].mileage();
for (int i = 1; i < cars.length; i++) {
int currentDistance = cars[i].mileage();
if (currentDistance > maxDistance) {
maxDistance = currentDistance;
winner = cars[i];
}
}
return winner;
}
}