Kotlin教程:when表达式(用于替换switch语句)

2020-04-2020:33:37编程语言入门到精通Comments1,703 views字数 1511阅读模式

Kotlin when表达式是一个返回值的条件表达式。 Kotlin when表达式用于替换switch语句。 Kotlin when表达式相当于其他语言(Java,C++,C)中的switch语句。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18255.html

使用when作为表达式

下面来看看一下表达式的简单示例。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18255.html

fun main(args: Array<String>){  
    var number = 4  
    var numberProvided = when(number) {  
        1 -> "One"  
        2 -> "Two"  
        3 -> "Three"  
        4 -> "Four"  
        5 -> "Five"  
        else -> "invalid number"  
    }  
    println("You provide $numberProvided")  
}
Kotlin

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18255.html

You provide Four

使用没有表达的when语句

使用when时,表达式也不是必须要使用的,因为它可以像在其他语言中一样正常使用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18255.html

例如 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18255.html

fun main(args: Array<String>){  

    var number = 4  
    when(number) {  
        1 -> println("One")  
        2 -> println("Two")  
        3 -> println("Three")  
        4 -> println("Four")  
        5 -> println("Five")  
        else -> println("invalid number")  
    }  

}
Kotlin

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18255.html

Four

when使用大括号的多重声明

可以使用条件块中包含的多个语句。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18255.html

例如 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18255.html

fun main(args: Array<String>){  
    var number = 1  
    when(number) {  
        1 -> {  
            println("Monday")  
            println("First day of the week")  
        }  
        7 -> println("Sunday")  
        else -> println("Other days")  
    }  
}
Kotlin

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18255.html

Monday
First day of the week

when 多个分支

以使用用逗号分隔的多个条件分支。当需要为多个选择运行相同的逻辑时就可以使用when多个分支。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18255.html

fun main(args: Array<String>){  
    var number = 8  
    when(number) {  
        3, 4, 5, 6 ->  
            println("It is summer season")  
        7, 8, 9 ->  
            println("It is rainy season")  
        10, 11 ->  
            println("It is autumn season")  
        12, 1, 2 ->  
            println("It is winter season")  
        else -> println("invalid input")  
    }  
}
Kotlin

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18255.html

It is rainy season

在范围内使用when

when表达式还检查条件提供的输入范围。 使用..(双点)运算符创建范围。 in运算符用于检查值是否属于某个范围。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18255.html

例如:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18255.html

fun main(args: Array<String>){  
    var number = 7  
    when(number) {  
        in 1..5 -> println("Input is provided in the range 1 to 5")  
        in 6..10 -> println("Input is provided in the range 6 to 10")  
        else -> println("none of the above")  
    }  
}
Kotlin

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18255.html

It is rainy season

//原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/kotlin/kotlin-when-expression.html文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18255.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18255.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/18255.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定