「Kotlin入門」when式にelseを使うサンプル

2020年11月6日

構文
when(条件){
数値1->xx
数値2->
else->
}
サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
when (x) {
11 -> print("x == 11")
22 -> print("x == 22")
else -> {
print("not 11 and 22")
}
}
when (x) { 11 -> print("x == 11") 22 -> print("x == 22") else -> { print("not 11 and 22") } }
when (x) {
    11 -> print("x == 11")
    22 -> print("x == 22")
    else -> { 
        print("not 11 and 22")
    }
}

 

Kotlin

Posted by arkgame