<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 程序:檢查字符是否為字母 > 原文: [https://www.programiz.com/kotlin-programming/examples/alphabet](https://www.programiz.com/kotlin-programming/examples/alphabet) #### 在此程序中,您將學習檢查給定字符是否為字母。 這是在 Kotlin 中使用`if else`語句或`when`表達式完成的。 ## 示例 1:使用`if`來檢查字母 ```kt fun main(args: Array<String>) { val c = '*' if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') println("$c is an alphabet.") else println("$c is not an alphabet.") } ``` 運行該程序時,輸出為: ```kt * is not an alphabet. ``` 與 Java 一樣,在 Kotlin 中,`char`變量存儲字符的 ASCII 值(0 到 127 之間的數字)而不是字符本身。 小寫字母的 ASCII 值從 97 到 122。大寫字母的 ASCII 值從 65 到 90。 這就是原因,我們在`'a'`(97)與`'z'`(122)之間比較變量`c`。 同樣,我們進行相同的操作以檢查`'A'`(65)至`'Z'`(90)之間的大寫字母。 以下是該程序的等效 Java 代碼:[檢查字符是否為字母的 Java 程序](/java-programming/examples/alphabet) * * * 您可以使用*范圍*代替比較來解決此問題。 ## 示例 2:使用`if else`和范圍來檢查字母 ```kt fun main(args: Array<String>) { val c = 'a' if (c in 'a'..'z' || c in 'A'..'Z') println("$c is an alphabet.") else println("$c is not an alphabet.") } ``` 運行該程序時,輸出為: ```kt a is an alphabet. ``` * * * 您甚至可以使用`when`表達式來代替問題。 ## 示例:使用`when`檢查字母 ```kt fun main(args: Array<String>) { val c = 'C' when { (c in 'a'..'z' || c in 'A'..'Z') -> println("$c is an alphabet.") else -> println("$c is not an alphabet.") } } ``` 運行該程序時,輸出為: ```kt C is an alphabet. ```
                  <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>

                              哎呀哎呀视频在线观看