-
Notifications
You must be signed in to change notification settings - Fork 716
/
Copy pathInput.kt
45 lines (39 loc) · 1.39 KB
/
Input.kt
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
import java.util.Scanner
class Input {
companion object {
var scanner = Scanner(System.`in`)
fun intInput(menuSize: Int): Int {
var input = -1
do {
try {
println("Введите номер пункта меню:")
input = scanner.nextLine().toInt()
if (input > menuSize) {
println("Такой пункт отсутствует")
input = -1
}
} catch (e: Exception) {
println("Необоходимо ввести число.")
}
} while (input < 0)
return input
}
fun textInput(prompt: String, maxLength: Int): String {
var input = ""
val max = maxLength
do {
println(prompt)
input = scanner.nextLine().trim()
when (input.length) {
in 1..max -> return input
in 0..0 -> println("Значение не может быть пустым")
else -> {
println("Введено слишком длинное значение")
input = ""
}
}
} while (input == "")
return input
}
}
}