【问题标题】:How to check a MutableList's element or a String input is a number?如何检查 MutableList 的元素或字符串输入是否为数字?
【发布时间】:2022-01-02 17:56:30
【问题描述】:

我在下面的代码中遇到了问题。

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循环的字符串,例如检查字段占用和坐标。提前致谢!

【问题讨论】:

  • 表是 char[][] - 对吧? char* pC = &amp;Table[0][0]; for(int i = 0; i&lt;9; i++){if(*pC = /*check for the ascii code - here you can define a range*/)throw new std::exception();}else{pC++;} 之类的东西,对不起,我有点脱离原生 C 代码,但应该看起来像这样。你可以做不同的事情,但请 - 如果你在矩阵中思考,请使用 foreach-loops - 不要做。
  • 对不起,我没有提到,代码在 Kotlin 中。我理解你的一些代码,但它是用 C 语言编写的。我看到你的 for 循环遍历表元素。但是我需要检查输入的是字符串还是数字,如果不是数字,则需要要求输入有效的数字然后用X或O更新表格。这部分代码现在不写了。跨度>
  • 感觉这样真的可以使用一些技巧来编写更好的代码,你可以尝试将这个(实现下面的答案)发布到 codereview.stackexchange.com,因为在这里指出有点太多了在 cmets 中。但至少我建议用枚举 { X,0,EMPTY } 为字段提供 3 个状态,而不是使用原始字符串,除非必须,否则不要使用 mutableLists,所以不要使用 split()
  • 好吧,我犯了一个错误,但人类可以犯错误。我的英语并不完美,因为它不是我的母语。但老实说,您可以看到 C 和 Kotlin 之间的区别!我很感激你的建议!但我需要以这种方式解决问题。如果知道使用数字,我认为没有人会愚蠢地使用字符串输入。但不是我写这个任务。我必须以书面方式解决它。检查输入是否不是字符串,检查是否超出范围,检查字段是否被占用。

标签: string function kotlin mutablelist


【解决方案1】:

我建议这样做

x = coordinates[0].toIntOrNull() ?: 99
y = coordinates[1].toIntOrNull() ?: 99

toInt()toIntOrNull() 的区别在于toIntOrNull() 在不能将其转换为 Int 时不会抛出异常,而是会返回 null。可以使用 elvis 运算符 ?: 将其重定向到备用号码。在这里,您可以放置​​任何不在您要求的 1-3 范围内的内容。我只是选择了 99 来证明这一点。

【讨论】:

  • 这确实是最简单的“即插即用”解决方案,但重构使 null 直接重试而不是使用幻数会更简洁
  • @somethingsomething true。我同意你关于还有更多改进空间的问题的评论。但我认为这超出了这个问题的范围。我通常避免提出需要进行重大重构的答案,因此给出了一个解决方案,可以尽可能少地完成这项工作
【解决方案2】:

我是这样做的:

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()
}

【讨论】:

  • 为什么你更喜欢这种方法而不是@Ivoks 以前的答案?
  • 只是想找到另一个解决方案。
  • 这似乎不适用于单个字母
猜你喜欢
  • 2011-07-22
  • 2014-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-09
  • 1970-01-01
相关资源
最近更新 更多