博客
关于我
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/

你可能感兴趣的文章
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>
MYSQL:基础——触发器
查看>>
Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 参数--lock-tables浅析
查看>>
mysqldump 导出中文乱码
查看>>
mysqldump 导出数据库中每张表的前n条
查看>>
mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
查看>>
Mysqldump参数大全(参数来源于mysql5.5.19源码)
查看>>
mysqldump备份时忽略某些表
查看>>
mysqldump实现数据备份及灾难恢复
查看>>
mysqldump数据库备份无法进行操作只能查询 --single-transaction
查看>>
mysqldump的一些用法
查看>>
mysqli
查看>>
MySQLIntegrityConstraintViolationException异常处理
查看>>