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

Kotlin cycle control

2022-07-04 19:42:00 AdleyTales

fun main() {
    

    val arr = arrayOf(1,2,3)
    for ((i,item) in arr.withIndex()){
    
        println("i:$i, item:$item")
    }
    /**
        i:0, item:1
        i:1, item:2
        i:2, item:3
     */

    val items = listOf("apple", "banana", "kiwi")
    for (item in items) {
    
        println(item)
    }

    for (i in items.indices){
    
        println("i: $i, value:${items[i]}")
    }
    /**
        i: 0, value:apple
        i: 1, value:banana
        i: 2, value:kiwi
     */

    // while
    var num =10
    var sum: Int = 0
    while (num > 0){
    
        sum += num
        num--
    }
    println("sum: $sum") // sum: 55

    val age = 12
    //var n = 5
    var n = -5
    do {
    
        println("age: $age, n: $n") // age: 12, n: -5
        n--
    }while (n > 0)
}

原网站

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