如何检查 MutableList 的元素或 String 输入是数字?
How to check a MutableList's element or a String input is a number?
我运行在下面的代码中遇到了问题。
fun main() {
val table = mutableListOf(
mutableListOf(' ', ' ', ' '),
mutableListOf(' ', ' ', ' '),
mutableListOf(' ', ' ', ' ')
)
println("---------")
println("| " + table[0][0] + " " + table[0][1] + " " + table[0][2] + " |")
println("| " + table[1][0] + " " + table[1][1] + " " + table[1][2] + " |")
println("| " + table[2][0] + " " + table[2][1] + " " + table[2][2] + " |")
println("---------")
print("Enter the coordinates: ")
var coordinates = readLine()!!.split(" ").toMutableList()
var x = coordinates[0].toInt()
var y = coordinates[1].toInt()
while (x > 3 || x < 1 || y > 3 || y < 1) {
println("Coordinates should be from 1 to 3!")
print("Enter the coordinates: ")
coordinates = readLine()!!.split(" ").toMutableList()
x = coordinates[0].toInt()
y = coordinates[1].toInt()
}
while (table[x-1][y-1] == 'X' || table[x-1][y-1] == 'O') {
println("This cell is occupied! Choose another one!")
print("Enter the coordinates: ")
coordinates = readLine()!!.split(" ").toMutableList()
x = coordinates[0].toInt()
y = coordinates[1].toInt()
}
table[x-1][y-1] = 'X'
println("---------")
println("| " + table[0][0] + " " + table[0][1] + " " + table[0][2] + " |")
println("| " + table[1][0] + " " + table[1][1] + " " + table[1][2] + " |")
println("| " + table[2][0] + " " + table[2][1] + " " + table[2][2] + " |")
println("---------")
if (table[0][0] == 'X' && table[0][1] == 'X' && table[0][2] == 'X' ||
table[1][0] == 'X' && table[1][1] == 'X' && table[1][2] == 'X' ||
table[2][0] == 'X' && table[2][1] == 'X' && table[2][2] == 'X' ||
table[0][0] == 'X' && table[1][0] == 'X' && table[2][0] == 'X' ||
table[0][1] == 'X' && table[1][1] == 'X' && table[2][1] == 'X' ||
table[0][2] == 'X' && table[1][2] == 'X' && table[2][2] == 'X' ||
table[0][0] == 'X' && table[1][1] == 'X' && table[2][2] == 'X' ||
table[2][0] == 'X' && table[1][1] == 'X' && table[0][2] == 'X'
) {
println("X wins")
} else if (table[0][0] == 'O' && table[0][1] == 'O' && table[0][2] == 'O' ||
table[1][0] == 'O' && table[1][1] == 'O' && table[1][2] == 'O' ||
table[2][0] == 'O' && table[2][1] == 'O' && table[2][2] == 'O' ||
table[0][0] == 'O' && table[1][0] == 'O' && table[2][0] == 'O' ||
table[0][1] == 'O' && table[1][1] == 'O' && table[2][1] == 'O' ||
table[0][2] == 'O' && table[1][2] == 'O' && table[2][2] == 'O' ||
table[0][0] == 'O' && table[1][1] == 'O' && table[2][2] == 'O' ||
table[2][0] == 'O' && table[1][1] == 'O' && table[0][2] == 'O'
) {
println("O wins")
}
}
我可以检查输入是否小于或大于 3(坐标),也可以检查该字段是否未被占用。但是我如何检查输入不是带有 while 循环的字符串,比如检查字段占用和坐标。提前致谢!
我建议这样做
x = coordinates[0].toIntOrNull() ?: 99
y = coordinates[1].toIntOrNull() ?: 99
与toInt()
和toIntOrNull()
的区别在于toIntOrNull()
在无法将其转换为Int但会return null时不会抛出异常反而。这可以使用 elvis 运算符 ?:
重定向到后备号码。在这里你可以放任何不在你要求的 1-3 范围内的东西。我只是选择 99 来演示这一点。
我是这样做的:
print("Enter the coordinates: ")
var coordinates = readLine()!!.split(" ").toMutableList()
while (coordinates[0].length > 1 || coordinates[1].length > 1 || coordinates[0].length > 1 && coordinates[1].length > 1 ) {
println("You should enter numbers!")
print("Enter the coordinates: ")
coordinates = readLine()!!.split(" ").toMutableList()
}
我运行在下面的代码中遇到了问题。
fun main() {
val table = mutableListOf(
mutableListOf(' ', ' ', ' '),
mutableListOf(' ', ' ', ' '),
mutableListOf(' ', ' ', ' ')
)
println("---------")
println("| " + table[0][0] + " " + table[0][1] + " " + table[0][2] + " |")
println("| " + table[1][0] + " " + table[1][1] + " " + table[1][2] + " |")
println("| " + table[2][0] + " " + table[2][1] + " " + table[2][2] + " |")
println("---------")
print("Enter the coordinates: ")
var coordinates = readLine()!!.split(" ").toMutableList()
var x = coordinates[0].toInt()
var y = coordinates[1].toInt()
while (x > 3 || x < 1 || y > 3 || y < 1) {
println("Coordinates should be from 1 to 3!")
print("Enter the coordinates: ")
coordinates = readLine()!!.split(" ").toMutableList()
x = coordinates[0].toInt()
y = coordinates[1].toInt()
}
while (table[x-1][y-1] == 'X' || table[x-1][y-1] == 'O') {
println("This cell is occupied! Choose another one!")
print("Enter the coordinates: ")
coordinates = readLine()!!.split(" ").toMutableList()
x = coordinates[0].toInt()
y = coordinates[1].toInt()
}
table[x-1][y-1] = 'X'
println("---------")
println("| " + table[0][0] + " " + table[0][1] + " " + table[0][2] + " |")
println("| " + table[1][0] + " " + table[1][1] + " " + table[1][2] + " |")
println("| " + table[2][0] + " " + table[2][1] + " " + table[2][2] + " |")
println("---------")
if (table[0][0] == 'X' && table[0][1] == 'X' && table[0][2] == 'X' ||
table[1][0] == 'X' && table[1][1] == 'X' && table[1][2] == 'X' ||
table[2][0] == 'X' && table[2][1] == 'X' && table[2][2] == 'X' ||
table[0][0] == 'X' && table[1][0] == 'X' && table[2][0] == 'X' ||
table[0][1] == 'X' && table[1][1] == 'X' && table[2][1] == 'X' ||
table[0][2] == 'X' && table[1][2] == 'X' && table[2][2] == 'X' ||
table[0][0] == 'X' && table[1][1] == 'X' && table[2][2] == 'X' ||
table[2][0] == 'X' && table[1][1] == 'X' && table[0][2] == 'X'
) {
println("X wins")
} else if (table[0][0] == 'O' && table[0][1] == 'O' && table[0][2] == 'O' ||
table[1][0] == 'O' && table[1][1] == 'O' && table[1][2] == 'O' ||
table[2][0] == 'O' && table[2][1] == 'O' && table[2][2] == 'O' ||
table[0][0] == 'O' && table[1][0] == 'O' && table[2][0] == 'O' ||
table[0][1] == 'O' && table[1][1] == 'O' && table[2][1] == 'O' ||
table[0][2] == 'O' && table[1][2] == 'O' && table[2][2] == 'O' ||
table[0][0] == 'O' && table[1][1] == 'O' && table[2][2] == 'O' ||
table[2][0] == 'O' && table[1][1] == 'O' && table[0][2] == 'O'
) {
println("O wins")
}
}
我可以检查输入是否小于或大于 3(坐标),也可以检查该字段是否未被占用。但是我如何检查输入不是带有 while 循环的字符串,比如检查字段占用和坐标。提前致谢!
我建议这样做
x = coordinates[0].toIntOrNull() ?: 99
y = coordinates[1].toIntOrNull() ?: 99
与toInt()
和toIntOrNull()
的区别在于toIntOrNull()
在无法将其转换为Int但会return null时不会抛出异常反而。这可以使用 elvis 运算符 ?:
重定向到后备号码。在这里你可以放任何不在你要求的 1-3 范围内的东西。我只是选择 99 来演示这一点。
我是这样做的:
print("Enter the coordinates: ")
var coordinates = readLine()!!.split(" ").toMutableList()
while (coordinates[0].length > 1 || coordinates[1].length > 1 || coordinates[0].length > 1 && coordinates[1].length > 1 ) {
println("You should enter numbers!")
print("Enter the coordinates: ")
coordinates = readLine()!!.split(" ").toMutableList()
}