You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin如何校验MutableList元素或字符串输入是否为数字

Kotlin井字棋非数字输入校验实现

Kotlin的String.toIntOrNull()方法可以安全转换字符串为整数,转换失败时返回null而非抛出异常,我们基于该特性新增非数字校验循环,放在所有校验逻辑最前面执行即可。以下是最小改动后的完整代码:

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

    var coordinates: MutableList<String>
    var x: Int? = null
    var y: Int? = null

    // 新增:非数字输入校验循环
    while (x == null || y == null) {
        print("Enter the coordinates: ")
        coordinates = readLine()!!.split(" ").toMutableList()
        x = coordinates.getOrNull(0)?.toIntOrNull()
        y = coordinates.getOrNull(1)?.toIntOrNull()
        if (x == null || y == null) {
            println("You should enter numbers!")
        }
    }

    // 原有坐标范围校验
    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.getOrNull(0)?.toIntOrNull()
        y = coordinates.getOrNull(1)?.toIntOrNull()
        while (x == null || y == null) {
            println("You should enter numbers!")
            print("Enter the coordinates: ")
            coordinates = readLine()!!.split(" ").toMutableList()
            x = coordinates.getOrNull(0)?.toIntOrNull()
            y = coordinates.getOrNull(1)?.toIntOrNull()
        }
    }

    // 原有单元格占用校验
    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.getOrNull(0)?.toIntOrNull()
        y = coordinates.getOrNull(1)?.toIntOrNull()
        while (x == null || y == null) {
            println("You should enter numbers!")
            print("Enter the coordinates: ")
            coordinates = readLine()!!.split(" ").toMutableList()
            x = coordinates.getOrNull(0)?.toIntOrNull()
            y = coordinates.getOrNull(1)?.toIntOrNull()
        }
        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.getOrNull(0)?.toIntOrNull()
            y = coordinates.getOrNull(1)?.toIntOrNull()
            while (x == null || y == null) {
                println("You should enter numbers!")
                print("Enter the coordinates: ")
                coordinates = readLine()!!.split(" ").toMutableList()
                x = coordinates.getOrNull(0)?.toIntOrNull()
                y = coordinates.getOrNull(1)?.toIntOrNull()
            }
        }
    }

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

如果需要优化重复代码,可以将输入校验逻辑封装为独立函数,避免多轮校验时重复编写相同代码。

内容的提问来源于stack exchange,提问作者Tibor Tóth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 08:15:02