0 原文

https://www.runoob.com/kotlin/kotlin-condition-control.html

使用区间,区间符号 …

使用 in 运算符来检测某个数字是否在指定区间内,区间格式为 x…y :

示例

fun main(args : Array<String>) {
    // 检测某个数字是否在指定区间内
    if (5 in 1..8) {
        println("数字 5 在[1, 8]区间内")
    } else {
        println("数字 5 不在[1, 8]区间内")
    }


    if (9 in 1..8) {
        println("数字 9 在[1, 8]区间内")
    } else {
        println("数字 9 不在[1, 8]区间内")
    }
}

运行结果

数字 5 在[1, 8]区间内
数字 9 不在[1, 8]区间内

Process finished with exit code 0
Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐