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
39 changes: 39 additions & 0 deletions src/main/java/Auto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.InputMismatchException;
import java.util.Scanner;

public class Auto {
String name = "";
int speed = 0;

public void getName(int i) {
Scanner scanner = new Scanner(System.in);
System.out.println("- Введите название машины №" + i + ":");
this.name = scanner.next();
while (this.name.isEmpty()) {
System.out.println("- Название машины не может быть пустой строкой");
System.out.println("- Введите название машины №" + i + ":");
this.name = scanner.next();
}
}

public void getSpeed (int i) {
boolean validSpeed = false;
Scanner scanner = new Scanner(System.in);

while (!validSpeed) {
try {
System.out.println("- Введите скорость машины №" + i + ":");
this.speed = scanner.nextInt();
while ((this.speed <= 0) || (this.speed > 250)) {
System.out.println("- Неправильная скорость");
System.out.println("- Введите скорость машины №" + i + ":");
this.speed = scanner.nextInt();
}
validSpeed = true;
} catch(InputMismatchException e) {
System.out.println("- Некорректный формат скорости");
scanner.next();
}
}
}
}
12 changes: 10 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Auto[] autoList = new Auto[3];
Race leMan = new Race();

for (int i = 1; i <= 3; i++) {
autoList[i-1] = new Auto();
autoList[i-1].getName(i);
autoList[i-1].getSpeed(i);
leMan.getLeader(autoList[i-1].name, autoList[i-1].speed);
}
System.out.println("Самая быстрая машина: " + leMan.leader);
}
}
11 changes: 11 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class Race {
String leader = "";
int leaderResult = 0;

public void getLeader (String name, int speed) {
if (24*speed > leaderResult) {
this.leader = name;
this.leaderResult = 24*speed;
}
}
}