当前位置:网站首页>Kotlin condition control

Kotlin condition control

2022-07-04 19:42:00 AdleyTales

fun main() {
    

    val a = 12
    val b = 5
    var c: Int
    var d: Int

    if (a > b) {
    
        c = 10000
    } else {
    
        c = 0
    }
    println("c: $c") // 10000

    // 2  Similar to ternary operators 
    d = if (a > b) 20000 else 0
    println("d: $d") // 20000

    println("------")

    // 3 IF  The result of the expression is assigned to a variable 
    val sum = if (a > b) {
    
        1000000
    } else {
    
        0
    }
    println(sum) // 1000000

    // 4  Section 
    val g = 13
    if (g in 1..18) {
    
        println(" A minor ")
    } else if (g > 18) {
    
        println(" adult ")
    }

    // 5 when expression 
    // when  Similar to other languages  switch  The operator 
    val x = 38
    when(x){
    
        0, 1 -> println(" Just born ")
        in 1..18 -> println(" A minor ")
        !in 20..28 -> println(" A minor 2")
        else -> {
    
            println(" Grown up ")
        }
    }
}
原网站

版权声明
本文为[AdleyTales]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041807344906.html