forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
81 lines (58 loc) · 1.94 KB
/
Main.java
File metadata and controls
81 lines (58 loc) · 1.94 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
import java.awt.*;
import java.util.Scanner;
public class Main {
public static int leaderDistance = 0;
public static String leaderName;
public static final int MAX_SPEED = 250;
public static final int MIN_SPEED = 0;
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
userInput();
System.out.println("Самая быстрая машина: " + leaderName);
}
private static void userInput(){
for (int i = 1; i < 4; i++) {
String name = "";
while (name.isEmpty()) {
System.out.println("Введите название машины №" + i + ":");
name = scanner.nextLine();
}
boolean checkEntry = true;
int speed = 0;
while (checkEntry){
System.out.println("Введите скорость машины №" + i + ":");
try {
speed = Integer.parseInt(scanner.nextLine());
if(speed < MIN_SPEED || speed > MAX_SPEED){
System.out.println("Неправильная скорость");
}
else{
checkEntry = false;
}
}
catch (NumberFormatException e){
System.out.println("Неверное значение");
}
}
Race.race (new Car(name, speed));
}
}
}
class Race {
private static final int RACE_TIME = 24;
public static void race(Car car) {
int distance = car.speed * RACE_TIME;
if(distance > Main.leaderDistance){
Main.leaderDistance = distance;
Main.leaderName = car.name;
}
}
}
class Car {
String name;
int speed;
public Car(String name, int speed) {
this.name = name;
this.speed = speed;
}
}