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 task #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
DmitriyP521 wants to merge 3 commits into dev
base: dev
Choose a base branch
Loading
from first-task
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
9 changes: 9 additions & 0 deletions src/main/java/Auto.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Auto {
String name;
int speed;

public Auto(String name, int speed) {
this.name = name;
this.speed = speed;
}
}
47 changes: 46 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,51 @@
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);
ArrayList<Auto> autoList = new ArrayList<>();
Auto leader;
int autoCount = 3;
for (int i=0; i < autoCount; i++) {
String name;
while (true) {
System.out.println("Введите название " + (i+1) + " автомобиля:");
name = scanner.nextLine().trim();
if (!name.isEmpty()) {
break;
} else {
System.out.println("Некорректно указано название автомобиля");
}
}
int speed;
String speedInput;
while (true) {
System.out.println("Введите скорость " + (i + 1) + " автомобиля:");
speedInput = scanner.nextLine().trim();
boolean isInt = true;
for (int j=0; j < speedInput.length(); j++) {
char c = speedInput.charAt(j);
if (c < '0' || c > '9') {
isInt = false;
}
}
if (speedInput.isEmpty() || !isInt) {
System.out.println("Некорректно указана скорость автомобиля");
} else {
speed = Integer.parseInt(speedInput);
if (speed > 0 && speed <= 250) {
break;
} else {
System.out.println("Некорректно указана скорость автомобиля");
}
}
}
Auto auto = new Auto(name, speed);
autoList.add(auto);
}
leader = Race.identifyLeader(autoList);
System.out.println("Самая быстрая машина: " + leader.name);
scanner.close();
}
}
16 changes: 16 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,16 @@
import java.util.ArrayList;

public class Race {
public static Auto identifyLeader(ArrayList<Auto> autoList) {
Auto leader = autoList.getFirst();
int maxDistance = leader.speed * 24;
for (Auto auto : autoList) {
int currentDistance = auto.speed * 24;
if (currentDistance > maxDistance) {
leader = auto;
maxDistance = currentDistance;
}
}
return leader;
}
}

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