android kotlin基础复习—for while do...while

1、新建一个文件kt:

2、循环的几种用法:

for

while

do ... while

for:

复制代码
println("----for 使用-----")
    val items = listOf("apple", "banana", "kiwi")
    for (item in items) {
        println(item)
    }

    for (index in items.indices) {
        println("item at $index is ${items[index]}")
    }

while:

复制代码
 println("----while 使用-----")
    var x = 5
    while (x > 0) {
        println( x--)
    }

do...while:

复制代码
println("----do...while 使用-----")
    var y = 5
    do {
        println(y--)
    } while(y>0)

3、测试代码:

复制代码
fun main(args: Array<String>) {
    println("----for 使用-----")
    val items = listOf("apple", "banana", "kiwi")
    for (item in items) {
        println(item)
    }

    for (index in items.indices) {
        println("item at $index is ${items[index]}")
    }

    println("----while 使用-----")
    var x = 5
    while (x > 0) {
        println( x--)
    }
    println("----do...while 使用-----")
    var y = 5
    do {
        println(y--)
    } while(y>0)

    println("----continue...break 使用-----")
    for (i in 1..10) {
        if (i==3) continue  // i 为 3 时跳过当前循环,继续下一次循环
        println(i)
        if (i>5) break   // i 为 6 时 跳出循环
    }

    println("---- 使用-----")
    foo()
    println("---- 使用-----")
    foo1()
    println("---- 使用-----")
    foo2()
    println("----for 使用-----")
    forLoop()
    println("----String length 使用-----")
    print(getStringLength("hello"))
}

fun foo() {
    val ints = intArrayOf(0,1,2)
    ints.forEach lit@ {
        if (it == 0) return@lit
        println(it)
    }
}

fun foo1() {
    val ints = intArrayOf(0,1,2)
    ints.forEach {
        if (it == 0) return@forEach
        println(it)
    }
}

fun foo2() {
    val ints = intArrayOf(0,1,2)
    ints.forEach(fun(value: Int) {
        if (value == 0) return
        println(value)
    })
}

fun forLoop(){
    print("循环输出:")
    for (i in 1..4) print(i) // 输出"1234"
    println("\n----------------")
    print("设置步长:")
    for (i in 1..4 step 2) print("$i ") // 输出"13"
    println("\n----------------")
    print("使用 downTo:")
    for (i in 4 downTo 1 step 2) print("$i ") // 输出"42"
    println("\n----------------")
    print("使用 until:")
    // 使用 until 函数排除结束元素
    for (i in 1 until 4) {   // i in [1, 4) 排除了 4
        print(i)
    }
    println("\n----------------")
}

fun getStringLength(obj: Any): Int? {
    if (obj is String) {
        // 做过类型判断以后,obj会被系统自动转换为String类型
        return obj.length
    }else{
        return 0
    }
}

4、测试结果:

相关推荐
JMchen1235 小时前
现代Android图像处理管道:从CameraX到OpenGL的60fps实时滤镜架构
android·图像处理·架构·kotlin·android studio·opengl·camerax
快点好好学习吧6 小时前
phpize 依赖 php-config 获取 PHP 信息的庖丁解牛
android·开发语言·php
是誰萆微了承諾6 小时前
php 对接deepseek
android·开发语言·php
Dxy12393102167 小时前
MySQL如何加唯一索引
android·数据库·mysql
冠希陈、9 小时前
PHP 判断是否是移动端,更新鸿蒙系统
android·开发语言·php
晚霞的不甘11 小时前
Flutter for OpenHarmony从零到一:构建《冰火人》双人合作闯关游戏
android·flutter·游戏·前端框架·全文检索·交互
2601_9498333911 小时前
flutter_for_openharmony口腔护理app实战+饮食记录实现
android·javascript·flutter
独自破碎E11 小时前
【滑动窗口+字符计数数组】LCR_014_字符串的排列
android·java·开发语言
stevenzqzq11 小时前
compose 中 align和Arrangement的区别
android·compose
VincentWei9512 小时前
Compose:MutableState 和 mutableStateOf
android