Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
55 lines (45 loc) · 1.64 KB
/
Copy pathMain.java
File metadata and controls
55 lines (45 loc) · 1.64 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Race currentRace = new Race();
int carSpeed;
for (int i = 1; i < 4; i++) {
System.out.println("— Введите название машины № " + i);
String carName = in.next();
System.out.println("— Введите скорость машины № " + i);
while (!in.hasNextInt()) {
System.out.println("— Неправильная скорость");
System.out.println("— Введите скорость машины № " + i);
in.next();
}
carSpeed = in.nextInt();
while (carSpeed < 0 || carSpeed > 250) {
System.out.println("— Неправильная скорость");
System.out.println("— Введите скорость машины № " + i);
carSpeed = in.nextInt();
}
Car currentCar = new Car(carName, carSpeed);
currentRace.setLeader(currentCar);
}
System.out.println("Самая быстрая машина: " + currentRace.leader.name);
}
}
class Car {
String name;
int speed;
public Car(String name, int speed) {
this.name = name;
this.speed = speed;
}
}
class Race {
Car leader;
int distance = 0;
public void setLeader (Car currentCar) {
if (distance < currentCar.speed*24) {
distance = currentCar.speed*24;
leader = currentCar;
}
}
}