Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
84 lines (68 loc) · 2.6 KB
/
Copy pathMain.java
File metadata and controls
84 lines (68 loc) · 2.6 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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 i = 0; i < 3; i++) {
System.out.println("Введите марку автомобиля " + (i+1) + ": ");
String model;
while (true) {
model = scanner.nextLine().trim();
if (!model.isEmpty()) {
break;
}
System.out.println("Марка автомобиля не может быть пустой строкой ");
}
int speed = 0;
while (true) {
System.out.println("Введите скорость автомобиля " + (i+1) + " в диапозоне от 0 до 250: ");
String input = scanner.nextLine();
try {
speed = Integer.parseInt(input);
if(speed >= 0 && speed <= 250){
break;
} else {
System.out.println("Некорректно введена скорость.");
}
} catch (NumberFormatException e) {
System.out.println("Некорректно введена скорость. Введите число.");
}
}
try {
cars[i] = new Car(model,speed);
}catch (Exception e){
System.out.println("Ошибка при создании объекта Car:");
e.printStackTrace();
}
}
int[] distances = new int[3];
for(int i = 0; i < 3; i++){
distances[i] = cars[i].totalDistance(24);
}
int maxDist = 0;
for(int i = 1; i < 3;i++) {
if(distances[i] > distances[maxDist]) {
maxDist = i++;
}
}
System.out.println("\nАвтомобиль, который проедет наибольшую дистанцию: " +
cars[maxDist].getModel() + ", с растоянием " + distances[maxDist] + " км");
}
static class Car {
private String model;
private int speed;
public Car(String model, int speed) {
this.model = model;
this.speed = speed;
}
public String getModel() {
return model;
}
public int getSpeed() {
return speed;
}
public int totalDistance(int hours) {
return speed * hours;
}
}
}