小编Ray*_*lim的帖子

当条件为真时,如何在when块中执行多个代码?

我知道它when { }可以用作 if-else 分支的替代品。但是,如果条件成立,是否可以执行多个分支?

例子:

val x = 5
val y = 4
    
when {
    x.isOdd() -> print("x is odd") //True, prints "x is odd"
    y.isEven() -> print("y is even") //Also True, but doesn't get executed
    else -> print("x+y is even.")
}
Run Code Online (Sandbox Code Playgroud)

有什么办法让第二行也执行吗?

kotlin

5
推荐指数
1
解决办法
1614
查看次数

标签 统计

kotlin ×1