<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Kotlin `when`表達式 > 原文: [https://www.programiz.com/kotlin-programming/when-expression](https://www.programiz.com/kotlin-programming/when-expression) #### 在本文中,您將借助各種示例來了解在 Kotlin 中的`when`結構。 ## Kotlin `when`結構 可以認為 Kotlin 中的`when`結構是 [Java `switch`語句](/java-programming/hello-world)的替代。 它求值許多替代方案中的一段代碼。 * * * ### 示例:簡單`when`表達式 ```kt fun main(args: Array<String>) { val a = 12 val b = 5 println("Enter operator either +, -, * or /") val operator = readLine() val result = when (operator) { "+" -> a + b "-" -> a - b "*" -> a * b "/" -> a / b else -> "$operator operator is invalid operator." } println("result = $result") } ``` 當您運行程序時,輸出將類似于: ```kt Enter operator either +, -, * or / * result = 60 ``` 上面的程序從用戶那里獲取輸入字符串(建議閱讀:[在 Kotlin 中獲取用戶的字符串輸入](/kotlin-programming/input-output#input-string))。 假設用戶輸入了`*`。 在這種情況下,對表達式`a * b`求值,并將該值分配給變量`result`。 如果不滿足任何分支條件(用戶輸入`+`,`-`,`*`或`/`除外),則求值`else`分支。 * * * 在上面的示例中,我們使用`when`作為表達式。 但是,使用`when`作為表達式不是強制性的。 例如, ```kt fun main(args: Array<String>) { val a = 12 val b = 5 println("Enter operator either +, -, * or /") val operator = readLine() when (operator) { "+" -> println("$a + $b = ${a + b}") "-" -> println("$a - $b = ${a - b}") "*" -> println("$a * $b = ${a * b}") "/" -> println("$a / $b = ${a / b}") else -> println("$operator is invalid") } } ``` 當您運行程序時,輸出將類似于: ```kt Enter operator either +, -, * or / - 12 - 5 = 7 ``` 此處,`when`不是表達式(`when`的返回值未分配給任何東西)。 在這種情況下,`else`分支不是必需的。 * * * ## 很小的可能性 **將兩個或多個分支條件與逗號組合。** 例如, ```kt fun main(args: Array<String>) { val n = -1 when (n) { 1, 2, 3 -> println("n is a positive integer less than 4.") 0 -> println("n is zero") -1, -2 -> println("n is a negative integer greater than 3.") } } ``` 運行該程序時,輸出為: ```kt n is a negative integer greater than 3. ``` * * * **檢查范圍內的值。** 例如, ```kt fun main(args: Array<String>) { val a = 100 when (a) { in 1..10 -> println("A positive number less than 11.") in 10..100 -> println("A positive number between 10 and 100 (inclusive)") } } ``` 運行該程序時,輸出為: ```kt A positive number between 10 and 100 (inclusive) ``` * * * **檢查值是否為特定類型。** 要在運行時檢查值是否為特定類型,我們可以使用[`is`和`!is`運算符](https://kotlinlang.org/docs/reference/typecasts.html)。 例如, ```kt when (x) { is Int -> print(x + 1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } ``` * * * **使用表達式作為分支條件。** 例如, ```kt fun main(args: Array<String>) { val a = 11 val n = "11" when (n) { "cat" -> println("Cat? Really?") 12.toString() -> println("Close but not close enough.") a.toString() -> println("Bingo! It's eleven.") } } ``` 運行該程序時,輸出為: ```kt Bingo! It's eleven. ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看