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

Проектная работа No1 #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
wyruk wants to merge 2 commits 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
10 changes: 10 additions & 0 deletions src/main/java/Cars.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class Cars {
String modelCar; // модель авто
int speedCar; // скорость авто

// Конструктор
Cars (String modelCar, int speedCar){
this.modelCar = modelCar;
this.speedCar = speedCar;
}
}
40 changes: 39 additions & 1 deletion src/main/java/Main.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
import java.util.Scanner;

public class Main {
public static final String pusto = "-------------------------------------\n"; // Разделитель

public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in); // Создаём экземпляр класса Scanner.

int maxCar = 3; // max авто
Cars[] cars = new Cars[maxCar]; // массив авто
Race race = new Race(); //объект Race

//Запрашиваем у пользователя 3 автомобиля
for (int i = 0; i < maxCar; i++) {
System.out.printf(Main.pusto + "— Введите название машины No %d: ", i + 1);
String model = scanner.nextLine();


//Проверяем, что введённая скорость >0 и ⩽250
int speed = 0;
while (true) {
System.out.printf("— Введите скорость машины No %d: ", i + 1);

if (scanner.hasNextInt()) {
speed = scanner.nextInt();
scanner.nextLine(); // очищаем отступ после nextInt
if (speed > 0 && speed <= 250) {
break;
} else {
System.out.println("— Неправильная скорость");
}
} else {
System.out.println("— Введите ниже целое число");
scanner.next(); // очищаем неверный ввод
}
}
cars[i] = new Cars(model, speed); // Добавляем в массив новую машину
}


System.out.println(Main.pusto);
race.resultRace(cars);
}
}
24 changes: 24 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,24 @@
public class Race {
void resultDay(Cars cars){
int resultForAllCars = cars.speedCar * 24;
System.out.printf("Машина [%s] проедет [%d] км за сутки.\n", cars.modelCar, resultForAllCars);
}
void resultRace(Cars[] cars) {
Cars leader = null;
int maxDistance = 0;

System.out.println("\nРезультаты гонки:");
for (Cars car : cars) {
resultDay(car);
int distance = car.speedCar * 24;
if (distance > maxDistance) {
maxDistance = distance;
leader = car;
}
}

if (leader != null) {
System.out.println("\nСамая быстрая машина: " + leader.modelCar);
}
}
}

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