forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckInput.java
More file actions
36 lines (33 loc) · 1.15 KB
/
CheckInput.java
File metadata and controls
36 lines (33 loc) · 1.15 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
import java.util.Scanner;
public class CheckInput {
Scanner scanner = new Scanner(System.in);
String carName;
int carSpeed;
String cancelInput = "Неправильный ввод. Попробуйте еще раз";
String checkInputName() {
while (true) {
if (!(scanner.hasNextInt())) {
carName = scanner.next();
if (carName.trim().isEmpty()) {
System.out.println("Вы ничего не ввели.");
} else return carName;
} else {
System.out.println(cancelInput);
}
scanner.next();
}
}
int checkInputSpeed() {
while (true) {
if (scanner.hasNextInt()) {
carSpeed = scanner.nextInt();
if (carSpeed < 0 || carSpeed > 250) {
System.out.println("Введите число, которое больше 0, но меньше или равно 250.");
} else return carSpeed;
} else {
System.out.println(cancelInput);
}
scanner.next();
}
}
}