<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # C# `switch`語句 > 原文: [https://www.programiz.com/csharp-programming/switch-statement](https://www.programiz.com/csharp-programming/switch-statement) #### 在本文中,我們將學習 C# 中的`switch`語句以及如何在示例中使用它們。 `switch`語句可用于替換 C# 中的`if...else if`語句。 使用`switch`語句的好處是,使用`switch`看起來代碼更清晰易讀。 `switch`語句的語法為: ```cs switch (variable/expression) { case value1: // Statements executed if expression(or variable) = value1 break; case value2: // Statements executed if expression(or variable) = value1 break; ... ... ... ... ... ... default: // Statements executed if no case matches } ``` `switch`語句求值`expression`(或`variable`),并將其值與每種情況下的值(或表達式)(`value1`,`value2`,…)。 當找到匹配值時,將執行該情況下的語句。 但是,如果以上情況均不匹配表達式,則將執行`default`塊中的語句。`switch`末尾的默認語句與`if else`語句中的`else`塊相似。 但是`switch`語句的問題是,當找到匹配值時,它將執行之后的所有語句,直到`switch`塊結束。 為了避免這種情況,我們在每種情況下使用`break`語句。`break`語句通過終止`switch`語句的執行來阻止程序執行不匹配的語句。 要了解有關`break`語句的更多信息,請訪問 *C# `break`語句*。 * * * ## 示例 1:C# `switch`語句 ```cs using System; namespace Conditional { class SwitchCase { public static void Main(string[] args) { char ch; Console.WriteLine("Enter an alphabet"); ch = Convert.ToChar(Console.ReadLine()); switch(Char.ToLower(ch)) { case 'a': Console.WriteLine("Vowel"); break; case 'e': Console.WriteLine("Vowel"); break; case 'i': Console.WriteLine("Vowel"); break; case 'o': Console.WriteLine("Vowel"); break; case 'u': Console.WriteLine("Vowel"); break; default: Console.WriteLine("Not a vowel"); break; } } } } ``` 當我們運行程序時,輸出將是: ```cs Enter an alphabet X Not a vowel ``` 在此示例中,提示用戶輸入字母。 如果字母為大寫,則使用`ToLower()`方法將其轉換為小寫。 然后,`switch`語句檢查用戶輸入的字母是否為`a, e, i, o or u`中的任何一個。 如果其中一種情況匹配,則打印`Vowel`,否則控制轉到默認塊,并且將`Not a vowel`打印為輸出。 由于所有元音的輸出都相同,因此我們可以將案例合并為: * * * ## 示例 2:帶有分組`case`的 C# `switch`語句 ```cs using System; namespace Conditional { class SwitchCase { public static void Main(string[] args) { char ch; Console.WriteLine("Enter an alphabet"); ch = Convert.ToChar(Console.ReadLine()); switch(Char.ToLower(ch)) { case 'a': case 'e': case 'i': case 'o': case 'u': Console.WriteLine("Vowel"); break; default: Console.WriteLine("Not a vowel"); break; } } } } ``` 兩個程序的輸出是相同的。 在上面的程序中,所有元音都打印輸出`Vowel`,并從`switch`語句中斷。 盡管`switch`語句使代碼看起來比`if...else if`語句更干凈,但是`switch`僅限于有限的數據類型。 C# 中的`switch`語句僅適用于: * 基本數據類型:布爾型,字符型和整數型 * *枚舉類型(枚舉)* * 字符串類 * 以上數據類型的可空類型 * * * ## 示例 3:使用 C# `switch`的簡單計算器程序 ```cs using System; namespace Conditional { class SwitchCase { public static void Main(string[] args) { char op; double first, second, result; Console.Write("Enter first number: "); first = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter second number: "); second = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter operator (+, -, *, /): "); op = (char)Console.Read(); switch(op) { case '+': result = first + second; Console.WriteLine("{0} + {1} = {2}", first, second, result); break; case '-': result = first - second; Console.WriteLine("{0} - {1} = {2}", first, second, result); break; case '*': result = first * second; Console.WriteLine("{0} * {1} = {2}", first, second, result); break; case '/': result = first / second; Console.WriteLine("{0} / {1} = {2}", first, second, result); break; default: Console.WriteLine("Invalid Operator"); break; } } } } ``` 當我們運行程序時,輸出將是: ```cs Enter first number: -13.11 Enter second number: 2.41 Enter operator (+, -, *, /): * -13.11 * 2.41 = -31.5951 ``` 上面的程序將兩個操作數和一個運算符作為用戶的輸入,并基于該運算符執行操作。 使用`ReadLine()`和`Read()`方法從用戶那里獲取輸入。 要了解更多信息,請訪問 [C# 基本輸入和輸出](/csharp-programming/basic-input-output "Basic input and output in C#")。 該程序使用`switch case`語句進行決策。 另外,我們可以使用`if-else if`梯形圖執行相同的操作。
                  <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>

                              哎呀哎呀视频在线观看