Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
63 lines (54 loc) · 2.15 KB
/
Copy pathMain.java
File metadata and controls
63 lines (54 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Vehicle> vehicleList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
System.out.println("Введите название машины №" + (i + 1));
String carName = scanner.next();
carName = carName + scanner.nextLine();
System.out.println("Введите скорость для машины №" + (i + 1));
while (true) {
if (scanner.hasNextInt()) {
int speed = scanner.nextInt();
if (speed > 0 && speed <= 250) {
scanner.nextLine();
vehicleList.add(new Vehicle(carName, speed));
Race.calculation(vehicleList.get(i));
break;
} else {
System.out.println("Неправильная скорость");
scanner.nextLine();
System.out.println("Введите скорость для машины №" + (i + 1));
}
} else {
System.out.println("Неправильная скорость");
scanner.next();
scanner.nextLine();
System.out.println("Введите скорость для машины №" + (i + 1));
}
}
}
System.out.println("Самая быстрая машина: " + Race.nameOfNewLeader);
}
}
class Vehicle {
String carName;
int speed;
public Vehicle(String carName, int speed) {
this.carName = carName;
this.speed = speed;
}
}
class Race {
static String nameOfNewLeader = "";
static int distance = 0;
public static void calculation(Vehicle vehicle) {
int distanceCalculation = vehicle.speed * 24;
if (distanceCalculation > distance) {
nameOfNewLeader = vehicle.carName;
distance = distanceCalculation;
}
}
}