zoukankan      html  css  js  c++  java
  • [Kotlin] More Advanced "when" Constructs

    Difference between "when" and Javascript "switch". 

    In Switch, you have to call "break"; but "when", you don't need to.

    In Switch, the case expression can only be string / integer value; but "when" can be any expressions.

    fun main() {
        val mode = 10
        
        when (mode) {
            5 -> println("mode is 5")
            3*5 -> println("mode is 15")
            "Hey there".length -> println("value is string")
            in 1..10 -> println("mode is between 1 and 10")
            in 11..20 -> println("mode is between 11 and 20")
            in 21..30 -> println("mode is between 31 and 30")
            !in 1..9 -> println("mode is not between 1 and 9")
        }
    
    }
    // mode is between 1 and 10
    fun main() {
        val random = Random().nextInt(50) + 1
    
        when (random) {
            in 1..10 -> println("In 1 to 10: $random")
            in 11..20 -> println("In 11 to 20: $random")
            in 21..30 -> println("In 21 to 30: $random")
            in 31..40 -> println("In 31 to 40: $random")
            else -> println("Over 40: $random")
        }
    
    } // In 31 to 40: 35
  • 相关阅读:
    AI CV 会议2018
    ubuntu 更改默认亮度
    ubuntu安装latex
    过滤文件代码 python
    ubuntu安装pycharm桌面快捷方式
    Ubuntu 14.04 鼠标消失解决方案
    ffmpeg常用命令
    FFMPEG 在ubuntu下的安装与使用
    pragma once
    chrono--高精度计时
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13796073.html
Copyright © 2011-2022 走看看