美文网首页
kotlin速成之道:if and when

kotlin速成之道:if and when

作者: 钱晓缺 | 来源:发表于2025-11-03 17:49 被阅读0次

```

var max = if (a > b) a else b

```

```

fun getDecsription(color : Color) : String =

    when(color){

        Blue -> 'cold'

        orange -> 'melt'

        red -> 'hot'

    }

```

check multiple cases.

```

fun getRes(input : String) : String = when(input) {

    'yes', 'y' -> 'yes'

    'no', 'n' -> 'no'

    else -> 'I don't know'

}

```

判断集合也是可以的。

使用is做判断也是可以的。

使用表达式同时吧when里面的参数去掉也是可以的

相关文章

网友评论

      本文标题:kotlin速成之道:if and when

      本文链接:https://www.haomeiwen.com/subject/jfhdfltx.html