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

First Project LeMan #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
Arkadiy186 wants to merge 1 commit into master
base: master
Choose a base branch
Loading
from devProject
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
82 changes: 81 additions & 1 deletion src/main/java/Main.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,8 +1,88 @@
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");
int maxCar = 3;
ArrayList<Automobile> automobileArrayList = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < maxCar; i++) {
System.out.println(String.format("Введите название автомобиля No%d", i + 1));
String name = " ";
while (true) {
if (scanner.hasNextInt()) {
System.out.println("Название автомобиля должно быть строкой");
scanner.next();
} else {
name = scanner.next();
break;
}
}
System.out.println(String.format("Введите скорость автомобиля No%d, в диапазоне от 0 до 250", i + 1));
int speed = -1;
while (speed < 0 || speed > 250) {
try {
speed = scanner.nextInt();
if (speed < 0 || speed > 250) {
System.out.println("Введите корректное число входящее в диапазон");
}
} catch (InputMismatchException e) {
System.out.println("Введите число");
scanner.nextLine();
}
}
automobileArrayList.add(new Automobile(name, speed));
}
Race race = new Race(automobileArrayList);
race.winnerIdentification();
System.out.println("Победитель гонки " + race.getWinnerName() + " скорость, которого составляла " + race.getWinnerSpeed() + " км/ч");
}
}

class Race {
ArrayList<Automobile> automobiles;
String winnerName = "";
int winnerSpeed = 0;
int distance = 0;
int time = 24;

public Race(ArrayList<Automobile> automobiles) {
this.automobiles = automobiles;
}
public void winnerIdentification() {
if (automobiles.isEmpty()) {
System.out.println("Автомобилей нет в гонке");
}
Automobile leader = automobiles.get(0);
for (Automobile automobile : automobiles) {
if(automobile.speed > leader.speed) {
leader = automobile;
}
}
distance = time * leader.speed;
winnerName = leader.name;
winnerSpeed = leader.speed;
}

public String getWinnerName() {
return winnerName;
}

public int getWinnerSpeed() {
return winnerSpeed;
}
}

class Automobile {
String name;
int speed;

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

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