kotlin中when的用法

楚天乐 2999 0 条

when是什么

when用于在kotlin中实现类c语言中的switch关键字

when怎么用

when (x) {
    1 -> print("x == 1")
    2 -> print("x == 2")
    else -> { // Note the block
        print("x is neither 1 nor 2")
    }
}

差不多是类C语言中这么个意思

swicth(x){
case 1:
    .......
    break;
case 2:
    .......
    break;
default: // kotlin else
    .......
    break;
}

帮助

https://kotlinlang.org/docs/reference/control-flow.html



与本文相关的文章

发表我的评论
昵称 (必填)
邮箱 (必填)
网址
执行时间: 1716262495842.9 毫秒