博客
关于我
Kotlin学习笔记3——条件控制
阅读量:376 次
发布时间:2019-03-05

本文共 2793 字,大约阅读时间需要 9 分钟。

Kotlin学习笔记3——条件控制

前言

上一篇,我们学习了,今天继续来学习Kotlin中条件控制。

IF 表达式

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

// 传统用法var max = a if (a < b) max = b// 使用 else var max: Intif (a > b) {       max = a} else {       max = b} // 作为表达式 表达式我们后面会详细讲,这里不理解可以先放着val max = if (a > b) a else b

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

val max = if (a > b) {       print("Choose a")    a} else {       print("Choose b")    b}

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

val c = if (condition) a else b

接下来就看一个实例

fun main(args: Array
) { var x = 0 if(x>0){ println("x 大于 0") }else if(x==0){ println("x 等于 0") }else{ println("x 小于 0") } var a = 1 var b = 2 val c = if (a>=b) a else b println("c 的值为 $c")}//输出结果为://x 等于 0//c 的值为 2

使用区间

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

fun main(args: Array
) { val x = 5 val y = 9 if (x in 1..8) { println("x 在区间内") }}//输入结果为:x 在区间内

When 表达式

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

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

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

//Lambda表达式我们后面再讲when (x) {       1 -> print("x == 1")    2 -> print("x == 2")    else -> {    // 注意这个块        print("x 不是 1 ,也不是 2")    }}

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

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

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

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

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

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

//这里Any是kotlin中任何类的超类,is关键字如果判断为true,会自动将x转为String类型fun hasPrefix(x: Any) = when(x) {       is String -> x.startsWith("prefix")    else -> false}

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

when {       x.isOdd() -> print("x is odd")    x.isEven() -> print("x is even")    else -> print("x is funny")}

下面看个实例:

fun main(args: Array
) { var x = 0 when (x) { 0, 1 -> println("x == 0 or x == 1") else -> println("otherwise") } when (x) { 1 -> println("x == 1") 2 -> println("x == 2") else -> { // 注意这个块 println("x 不是 1 ,也不是 2") } } when (x) { in 0..10 -> println("x 在该区间范围内") else -> println("x 不在该区间范围内") }}

输出结果:

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

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

fun main(args: Array
) { val items = setOf("apple", "banana", "kiwi") when { "orange" in items -> println("juicy") "apple" in items -> println("apple is fine too") }}//输出结果:apple is fine too

尾巴

今天的学习笔记就先到这里了,下一篇我们将学习。

老规矩,喜欢我的文章,欢迎素质三连:点赞,评论,关注,谢谢大家!

转载地址:http://ukwwz.baihongyu.com/

你可能感兴趣的文章
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>
MySQL Connector/Net 句柄泄露
查看>>
multiprocessor(中)
查看>>
mysql CPU使用率过高的一次处理经历
查看>>
Multisim中555定时器使用技巧
查看>>
MySQL CRUD 数据表基础操作实战
查看>>
multisim变压器反馈式_穿过隔离栅供电:认识隔离式直流/ 直流偏置电源
查看>>
mysql csv import meets charset
查看>>
multivariate_normal TypeError: ufunc ‘add‘ output (typecode ‘O‘) could not be coerced to provided……
查看>>
MySQL DBA 数据库优化策略
查看>>
multi_index_container
查看>>
MySQL DBA 进阶知识详解
查看>>
Mura CMS processAsyncObject SQL注入漏洞复现(CVE-2024-32640)
查看>>