【3】Kotlin学习之循环以及kotlin中的三元运算

Kotlin使用when代替switch 并且功能更加强大。

方法如下

        when(a){
            1->"result 1" 
            1,2->"1 or 2"
            in 1..10->">=1 or <=10"
            !in 1..10->"<1 or >10"
            is Int->"Integer"
            else->"Default"
        }

kotlin中的三元运算

        var a = 10
        var b = 8
        var result = if(a>b){a}else{b} //Kotlin中的三目运算  最后结果 8

Kotlin中的for循环 while。

for (item in collection)  //类似foreach 
    print(item)

while do while

while (x > 0) {
    x--
}

do {
    x++;
} while (x>?) //  跟java没区别

猜你喜欢

转载自blog.csdn.net/a940659387/article/details/78719650