Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
53 lines (51 loc) · 1.78 KB
/
Copy pathMain.java
File metadata and controls
53 lines (51 loc) · 1.78 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
import java.util.Scanner;
public class Main {
public static class Car {
String name;
int speed;
Car(String name, int speed) {
this.name = name;
this.speed = speed;
}
}
public static class Race {
String leaderName;
int leaderDistance;
void NewLeaderName(Car car) {
int distance = 24 * car.speed;
if (distance > leaderDistance) {
leaderDistance = distance;
leaderName = car.name;
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Race race = new Race();
System.out.println("Введите названия и скорости 3-x автомобилей:");
for ( int i = 1; i <= 3; i++) {
System.out.println("Название " + i + " автомобиля:");
String name = scanner.nextLine();
System.out.println("Скорость(от 0 до 250):");
int speed;
while (true) {
if (scanner.hasNextInt()) {
speed = scanner.nextInt();
if (speed >= 0 && speed <= 250) {
break;
} else {
System.out.println("Введите число от 0 до 250!");
}
} else {
System.out.println("Введите целое число!");
scanner.next();
}
}
Car car = new Car(name, speed);
race.NewLeaderName(car);
scanner.nextLine();
}
System.out.println("Самая быстрая машина: " + race.leaderName);
scanner.close();
}
}