-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathMain.java
More file actions
59 lines (49 loc) · 1.85 KB
/
Copy pathMain.java
File metadata and controls
59 lines (49 loc) · 1.85 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Race race = new Race();
for (int i = 1; i <= 3; i++) {// 3 машины
System.out.println("Введите название машины №" + i + ":");
String name = scanner.next();
int speed = 0; //скорость
while (true) {
System.out.println("Введите скорость машины №" + i + ":");
if (scanner.hasNextInt()) {// проверка целого числа
speed = scanner.nextInt();
if (speed > 0 && speed <= 250) {
break;
} else {
System.out.println("Неправильная скорость. Введите значение от 1 до 250.");
}
} else {
System.out.println("Неправильная скорость. Введите целое число.");
scanner.next();
}
}
Car car = new Car(name, speed);//автомобиль
race.checkLeader(car);
}
System.out.println("Самая быстрая машина: " + race.leaderName);//победитель
scanner.close();
}
}
class Car {//класс авто
String name;
int speed;
public Car(String name, int speed) {
this.name = name;
this.speed = speed;
}
}
class Race {//класс гонка
String leaderName = "";
int leaderDistance = 0;
public void checkLeader(Car car) {
int distance = car.speed * 24;
if (distance > leaderDistance) {
leaderDistance = distance;
leaderName = car.name;
}
}
}