Kotlin 检查一个数字(给定数字=100)是否大于或等于100、小于100且大于50,或小于或等于50

Kotlin 检查一个数字(给定数字=100)是否大于或等于100、小于100且大于50,或小于或等于50,kotlin,Kotlin,这里我要问的问题是,我们是否可以使用when语句,它是kotlin中所有嵌套if语句的表达式体 fun check(num: Int) = when { num >= 100 -> "$num is greater than or equal to 100" num > 50 -> "$num is less than 100 and greater than 50" else -> "$nu

这里我要问的问题是,我们是否可以使用when语句,它是kotlin中所有嵌套if语句的表达式体

fun check(num: Int) = when {
    num >= 100 -> "$num is greater than or equal to 100"
    num > 50 -> "$num is less than 100 and greater than 50"
    else -> "$num is less than or equal to 50"
}

fun main() {
    println(check(200))
    println(check(100))
    println(check(70))
    println(check(51))
    println(check(50))
    println(check(30))
}
输出:

200 is greater than or equal to 100
100 is greater than or equal to 100
70 is less than 100 and greater than 50
51 is less than 100 and greater than 50
50 is less than or equal to 50
30 is less than or equal to 50

请提供您以前的尝试。