<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Kotlin 程序:計算句子中元音和輔音的數量 > 原文: [https://www.programiz.com/kotlin-programming/examples/vowel-consonant-count-string](https://www.programiz.com/kotlin-programming/examples/vowel-consonant-count-string) #### 在此程序中,您將學習計算 Kotlin 中給定句子中的元音,輔音,數字和空格的數量。 ## 示例 1:計算元音,輔音,數字和空格的程序 ```kt fun main(args: Array<String>) { var line = "This website is aw3som3." var vowels = 0 var consonants = 0 var digits = 0 var spaces = 0 line = line.toLowerCase() for (i in 0..line.length - 1) { val ch = line[i] if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { ++vowels } else if (ch in 'a'..'z') { ++consonants } else if (ch in '0'..'9') { ++digits } else if (ch == ' ') { ++spaces } } println("Vowels: $vowels") println("Consonants: $consonants") println("Digits: $digits") println("White spaces: $spaces") } ``` 運行該程序時,輸出為: ```kt Vowels: 6 Consonants: 11 Digits: 3 White spaces: 3 ``` 在上面的示例中,每個檢查都有 4 個條件。 * 第一個`if`條件是檢查字符是否為**元音**。 * `if`后面的`else if`條件是檢查字符是否為**輔音**。 否則順序應相同,否則所有元音也被視為輔音。 * 第三個條件(`else-if`)是檢查字符是否在 **0 到 9** 之間。 * 最后,最后一個條件是檢查字符是否為**空格**字符。 為此,我們使用`toLowerCase()`將行小寫。 這樣做是為了不檢查大寫的 A 到 Z 和元音的優化。 我們使用`length()`函數來了解字符串的長度,并使用`charAt()`來獲取給定索引(位置)處的字符。 這是等效的 Java 代碼: [Java 程序:計算句子中的元音和輔音數量](/java-programming/examples/vowel-consonant-count-string "Java program to count number of vowels and consonants in a sentence")。 * * * ## 示例 2:使用`when`計數元音,輔音,數字和空格的程序 ```kt fun main(args: Array<String>) { var line = "This website is aw3som3." var vowels = 0 var consonants = 0 var digits = 0 var spaces = 0 line = line.toLowerCase() for (i in 0..line.length - 1) { val ch = line[i] when (ch) { 'a', 'e', 'i', 'o', 'u' -> ++vowels in 'a'..'z' -> ++consonants in '0'..'9' -> ++digits ' ' -> ++spaces } } println("Vowels: $vowels") println("Consonants: $consonants") println("Digits: $digits") println("White spaces: $spaces") } ``` 該程序的輸出與示例 1 相同。 在這里,您可以看到我們使用了一個簡單的`when`表達式來刪除`if-else`語句。 這使得代碼少了很多,也更容易理解。
                  <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>

                              哎呀哎呀视频在线观看