Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
70 lines (52 loc) · 1.81 KB
/
Copy pathMain.java
File metadata and controls
70 lines (52 loc) · 1.81 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
64
65
66
67
68
69
70
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Car[] cars = new Car[3];
for (int j = 0; j < 3; j++) {
System.out.println("Введите название машины № " + (j + 1));
String name = scanner.next();
System.out.println("Введите скорость машины № " + (j + 1));
int speed = 0;
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
speed = scanner.nextInt();
if (speed >= 0 && speed <= 250) {
break;
}
else {
System.out.println("Введите корректное значение скорости от 0 до 250 км/час");
}
}
else {
String s = scanner.next();
System.out.println("Введите корректное значение");
}
}
cars[j] = new Car(name, speed);
}
scanner.close();
String winner = cars[0].name;
int winDistance = cars[0].countDistance();
for (int i = 0; i < 3; i++) {
if (cars[i].countDistance() > winDistance) {
winDistance = cars[i].countDistance();
winner = cars[i].name;
}
}
System.out.println("Победитель гонки: " + winner);
}
}
class Car {
String name;
int speed;
Car(String name, int speed) {
this.name = name;
this.speed = speed;
}
int distance = 0;
int countDistance() {
distance = 24 * speed;
return distance;
}
}