Example if: fun getScore(name: String) = if (name == "Tom") 86 else if (name == "Jim") 77 else if (name == "Jack") 95 else if (name == "Lily") 100 else 0 1. 2. 3. 4. 5. Example when: fun getScore1(name: String) = when (name) {...
packagenet.println.kotlin.chapters /** *@author:wangdong *@description:`WhenExample.kt` */ classWhenExample{ enumclassState{ IDLE, BUFFERING, PLAYING, PAUSED } privatevarstate = State.IDLE funpause(){ when(state) { WhenExample.State.BUFFERING, WhenExample.State.PLAYING -> doPause() else->...
1.在使用kotlin的id之前,需要先在builde.gradle里引入这个 2.然后再在activity里面引入: 其中,activity_start就...kotlin 前言:懂得珍惜方可拥有更多。 一、安装jdk1.8+ 二、安装 IntelliJ IDEA, version15+ IntelliJ IDEA下载地址: 三、在IntelliJ IDE中新建kotlin项目 1、新建 2、选择Project SDK,也就是设置...
same usage as with the first example. However: I wouldn't use this approach except you have a really good reason for it. The reason I wouldn't use it: it requires you to always remember to adapt both, the enum value and its corresponding counterpart in the fromValue-function. So you ...
在Kotlin中其实是不存在三元运算符(condition ? then : else)这种操作的。 那是因为if语句的特性(if表达式会返回一个值)故而不需要三元运算符。 例: // 在Java中可以这么写,但是Kotlin中直接会报错。 // var numB: Int = (numA > 2) ? 3 : 5 ...
packagenet.println.kotlin.chapters/** * @author:wangdong * @description:`WhenExample.kt` */fun main(args:Array<String>) {valx =5when(x){//是否是intisInt-> println("hello $x")//是在区间范围内in1..100-> println("$x 在1-100区间内")//不在区间内!in1..100-> println("$x 不在...
Kotlin when Construct The when construct in Kotlin can be thought of as a replacement for Java switch Statement. It evaluates a section of code among many alternatives. Example: Simple when Expression fun main(args: Array<String>) { val a = 12 val b = 5 println("Enter operator either +...
Kotlin whenInstead of writing many if..else expressions, you can use the when expression, which is much easier to read.It is used to select one of many code blocks to be executed:Example Use the weekday number to calculate the weekday name: val day = 4 val result = when (day) { ...
We can also use the in-operator function with the kotlin multiple condition functions. In the example below, we are using an operator inside the when block to check whether the specified value is present or not in a range. Code: fun main(args : Array<String>) { ...
In the example, we generate a random number. Based on the random value, we print a message to the console. Kotlin while loopThe while keyword is used to create a loop. It runs until the given condition is met. while_loop.kt package com.zetcode fun main() { var i:Int = 0 while(...