Repository navigation
Expand file tree
/
Copy pathRace.java
More file actions
55 lines (52 loc) · 2.61 KB
/
Copy pathRace.java
File metadata and controls
55 lines (52 loc) · 2.61 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.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Race {
ArrayList<Car> car = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
public void start() {
for (int i = 1; i <= 3; i++) {
String mark;
while (true) {
System.out.println("Укажите название машины №" + i);
mark = scanner.next();
if (!mark.isEmpty()) {
break;
} else {
System.out.println("Название не подходит, попробуем другое");
}
}
int speed;
while (true) {
System.out.println("Укажите скорость машины №" + i);
try {
speed = scanner.nextInt();
if ((speed > 0) && (speed <= 250)) {
break;
} else {
System.out.println("Скорость не входит в диапазон от 0 до 250, попробуйте снова");
}
} catch (InputMismatchException e) {
System.out.println("Укажите скорость в числовом формате");
scanner.nextLine();
}
}
car.add(new Car(mark, speed));
System.out.println(mark + " Машина №" + i + " едет со скоростью - " + speed);
}
}
public void finish() {
int firstCarDistance = car.get(0).speed *24;
int secondCarDistance = car.get(1).speed *24;
int thirdCarDistance = car.get(2).speed *24;
if (firstCarDistance > secondCarDistance && firstCarDistance > thirdCarDistance){
System.out.println(car.get(0).mark + " проехала - " + firstCarDistance + " км за 24 часа и Выйграла гонку!");
} else if (secondCarDistance > firstCarDistance && secondCarDistance > thirdCarDistance){
System.out.println(car.get(1).mark + " проехала - " + secondCarDistance + " км за 24 часа и Выйграла гонку!");
} else if(thirdCarDistance > firstCarDistance && thirdCarDistance > secondCarDistance){
System.out.println(car.get(2).mark + " проехала - " + thirdCarDistance + " км за 24 часа и Выйграла гонку!");
} else{
System.out.println("В этот раз на гонке нет одного победителя...");
}
}
}