Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
64 lines (56 loc) · 1.87 KB
/
Copy pathMain.java
File metadata and controls
64 lines (56 loc) · 1.87 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
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++){
System.out.println("- Введите название машины №" + i);
String name;
while (true){
name = scanner.nextLine();
if (!name.trim().isEmpty()){
break;
} else {
System.out.println("- Неверное название!");
}
}
System.out.println("- Введите скорость машины №" + i);
int speed;
while (true){
if(scanner.hasNextInt()){
speed = scanner.nextInt();
if(speed > 0 && speed <= 250){
scanner.nextLine();
break;
} else {
System.out.println("- Неверная скорость!");
}
} else {
System.out.println("- Нужно ввести целое число");
scanner.next(); //загуглил, что стоит очищать буффер
}
}
Car car = new Car(name, speed);
race.getWinner(car);
}
System.out.println("Победитель: " + race.winner);}
}
class Car{
int speed = 0;
String name;
Car(String name, int speed){
this.name = name;
this.speed = speed;
}
}
class Race{
String winner = "";
int distance = 0;
public void getWinner(Car car) {
int distance = car.speed * 24;
if (distance > this.distance){
this.distance = distance;
winner = car.name;
}
}
}