Kotlin 条件控制

偏执的太偏执、 2022-11-26 08:42 237阅读 0赞

Kotlin 条件控制

  • IF 表达式
    • 示例
  • 使用区间
    • 示例
  • When 表达式
    • 示例
  • END

IF 表达式

一个 if 语句包含一个布尔表达式和一条或多条语句。

  1. // 传统用法
  2. var max = a
  3. if (a < b) max = b
  4. // 使用 else
  5. var max: Int
  6. if (a > b) {
  7. max = a
  8. } else {
  9. max = b
  10. }
  11. // 作为表达式
  12. val max = if (a > b) a else b

我们也可以把 IF 表达式的结果赋值给一个变量。

  1. val max = if (a > b) {
  2. print("Choose a")
  3. a
  4. } else {
  5. print("Choose b")
  6. b
  7. }

不需要像Java那种有一个三元操作符,因为我们可以使用它来简单实现:

  1. val c = if (condition) a else b

示例

  1. fun main(args: Array<String>) {
  2. var x = 0
  3. if(x>0){
  4. println("x 大于 0")
  5. }else if(x==0){
  6. println("x 等于 0")
  7. }else{
  8. println("x 小于 0")
  9. }
  10. var a = 1
  11. var b = 2
  12. val c = if (a>=b) a else b
  13. println("c 的值为 $c")
  14. }

输出结果

  1. x 等于 0
  2. c 的值为 2

使用区间

使用 in 运算符来检测某个数字是否在指定区间内,区间格式为 x…y :

示例

  1. fun main(args: Array<String>) {
  2. val x = 5
  3. val y = 9
  4. if (x in 1..8) {
  5. println("x 在区间内")
  6. }
  7. }

输出结果

  1. x 在区间内

When 表达式

when 将它的参数和所有的分支条件顺序比较,直到某个分支满足条件。

when 既可以被当做表达式使用也可以被当做语句使用。如果它被当做表达式,符合条件的分支的值就是整个表达式的值,如果当做语句使用, 则忽略个别分支的值。

when 类似其他语言的 switch 操作符。其最简单的形式如下:

  1. when (x) {
  2. 1 -> print("x == 1")
  3. 2 -> print("x == 2")
  4. else -> {
  5. // 注意这个块
  6. print("x 不是 1 ,也不是 2")
  7. }
  8. }

在 when 中,else 同 switch 的 default。如果其他分支都不满足条件将会求值 else 分支。

如果很多分支需要用相同的方式处理,则可以把多个分支条件放在一起,用逗号分隔:

  1. when (x) {
  2. 0, 1 -> print("x == 0 or x == 1")
  3. else -> print("otherwise")
  4. }

我们也可以检测一个值在(in)或者不在(!in)一个区间或者集合中:

  1. when (x) {
  2. in 1..10 -> print("x is in the range")
  3. in validNumbers -> print("x is valid")
  4. !in 10..20 -> print("x is outside the range")
  5. else -> print("none of the above")
  6. }

另一种可能性是检测一个值是(is)或者不是(!is)一个特定类型的值。注意: 由于智能转换,你可以访问该类型的方法和属性而无需 任何额外的检测。

  1. fun hasPrefix(x: Any) = when(x) {
  2. is String -> x.startsWith("prefix")
  3. else -> false
  4. }

when 也可以用来取代 if-else if链。 如果不提供参数,所有的分支条件都是简单的布尔表达式,而当一个分支的条件为真时则执行该分支:

  1. when {
  2. x.isOdd() -> print("x is odd")
  3. x.isEven() -> print("x is even")
  4. else -> print("x is funny")
  5. }

示例

  1. fun main(args: Array<String>) {
  2. var x = 0
  3. when (x) {
  4. 0, 1 -> println("x == 0 or x == 1")
  5. else -> println("otherwise")
  6. }
  7. when (x) {
  8. 1 -> println("x == 1")
  9. 2 -> println("x == 2")
  10. else -> {
  11. // 注意这个块
  12. println("x 不是 1 ,也不是 2")
  13. }
  14. }
  15. when (x) {
  16. in 0..10 -> println("x 在该区间范围内")
  17. else -> println("x 不在该区间范围内")
  18. }
  19. }

输出结果

  1. x == 0 or x == 1
  2. x 不是 1 ,也不是 2
  3. x 在该区间范围内

when 中使用 in 运算符来判断集合内是否包含某实例:

  1. fun main(args: Array<String>) {
  2. val items = setOf("apple", "banana", "kiwi")
  3. when {
  4. "orange" in items -> println("juicy")
  5. "apple" in items -> println("apple is fine too")
  6. }
  7. }

输出结果:

  1. apple is fine too

END

发表评论

表情:
评论列表 (有 0 条评论,237人围观)

还没有评论,来说两句吧...

相关阅读

    相关 IF条件控制

    条件控制 定义 Python 条件语句是通过一条或多条语句的执行结果(True 或者 False)来决定执行的代码块。 如下图所示 ![1697104-2019