Kotlin 03.if - else表达式示例 知识聚变 微信公众号:知识聚变 来自专栏 · 知识聚变 Kotlin 03.if - else表达式示例 前言: 今天,我们将学习使用 Kotlin 中的if-else 表达式示例,if-else 表达式是很多编程语言的逻辑控制语句,但是 Kotlin 的if-else 表达式有一些不一样,就是if-else 表达式可以直接赋值给变量 0
1.if / else if语句 和Java相同 代码语言:javascript 代码运行次数:0 运行 AI代码解释 fun main() { var gender = 0 if (gender == 0) { print("男") } else { print("女") } } 2.range表达式 可以判断一个元素是否在集合里: [item] in [start]..[end] 代码语言:javascript 代码运行次数:0...
fun Big(x:Int,y:Int,z:Int):Int {if(x>y) {if(x<z) {returnz }else{returnx } }else{if(y>z) {returny; }else{returnz } }returny } 这个就是一个if else嵌套。 以上就是if 的单分支 多分支 双分支的用法。 也就是传统的用法。 那么我之前说过 if可以代替: ?这个判断。 而这是什么意...
//Java中的ifintscore = 90;if(score >= 90 && score <= 100) { System.out.println("优秀"); }elseif(score >= 80 && score <= 89) { System.out.println("良好"); }elseif(score >= 60 && score <= 79) { System.out.println("及格"); }elseif(score >= 0 && score <= 59) { ...
1 先讲讲kotlin if 语句的基本使用,这个和java是一样的if表达式是我们常用到的判断执行语句,if条件判断,所以语法是if之后跟(布尔值)来决定是否执行if 下面跟的语句,假如布尔值为True,这执行下面的语法块,否则,不执行 2 if 单一的判断假如不满足您的需求,您可以添加else语法,也就是布尔值为False的时候...
(1)、简单分支–if…else kotlin 中 if…else 语句具有返回值,类似于Java中的三目运算 代码语言:javascript 代码运行次数:0 运行 AI代码解释 //传入两个值 a 和 b , 将大值显示在TextView控件中nameTextView.text=if(a>b)aelseb (2)、多分支 ...
kotlin 中的三元运算符表达式写法 if ( a > b) a else b when 条件语句 fun main(args : Array<String>){var week : Int = 3when(week){1 -> print("星期一")2 -> print("星期二")3 -> print("星期三")4 -> print("星期四")5 -> print("星期五")6 -> print("星期六")7 -> prin...
4. Kotlin If-Else If-Else Ladder ExpressionsWe can use the if-else..if-else ladder expression for checking multiple conditions. These conditions are executed from top to bottom.When a condition is true, it executes the corresponding if expression. If none of the conditions is true, it ...
和if表达式一样,when表达式也是带有返回值的。建议对于多层条件级或嵌套条件控制的使用建议使用when替代if-else: fun eval(number: Number) { if (number is Int) { println("this is int number") } else if (number is Double) { println("this is double number") ...
if (age > 18) { println("You can obtain a driving licence") } else { println("You cannot obtain a driving licence") } This condition tests if the age is greater than 18. Kotlin if else ifMultiple branches of conditions can be created with if else if syntax. ...