溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

十四、流程控制之switch語句

發(fā)布時(shí)間:2020-07-06 13:31:12 來源:網(wǎng)絡(luò) 閱讀:498 作者:vik_xiao 欄目:編程語言
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _14.流程控制之switch語句
{
    class Program
    {
        static void Main(string[] args)
        {
            // switch語句
            
            /**
             * switch語句可以一次將測試變量與多個(gè)值進(jìn)行比較,而不是僅測試一個(gè)條件。
             * switch語句語法:
             *  switch (<testVar>)
             *  {
             *      case <comparisonVar1>:
             *          <code to execute if <testVar> == <comparisonVar1>>
             *          break;
             *      case <comparisonVar2>:
             *          <code to execute if <testVar> == <comparisonVar2>>
             *          break;
             *      ...
             *      case <comparisonVarN>:
             *          <code to execute if <testVar> == <comparisonVarN>>
             *          break;
             *      default:
             *          <code to execute if <testVar> != <comparisonVars>>
             *          break;
             *  }
             *  
             * 解釋說明:
             *  1. <testVar>中的值與每個(gè)<comparisonVarX>值(在case語句中指定),如果有一個(gè)匹配,
             *     就執(zhí)行為該匹配提供的語句。如果沒有匹配,就執(zhí)行default部分中的代碼。
             *  2. 執(zhí)行完每個(gè)部分中的代碼后,還需要另一個(gè)語句break。
             *  3. 在執(zhí)行完一個(gè)case塊后,再執(zhí)行第二個(gè)case語句是非法的。
             *  4. 每個(gè)<comarisonVarX>都必須是一個(gè)常數(shù)值(字面值或常量)。
             *  5. switch語句對(duì)case語句的數(shù)量沒有限制。
             *  
             * 可以使用三種方式防止程序流程從一個(gè)case語句轉(zhuǎn)到下一個(gè)case語句:
             * 1. break語句將中斷switch語句的執(zhí)行,而執(zhí)行該結(jié)構(gòu)后面的語句。
             * 2. return語句將中斷當(dāng)前函數(shù)的運(yùn)行,而不是僅中斷switch結(jié)構(gòu)的執(zhí)行。
             * 3. 可以使用goto語句跳轉(zhuǎn)到其他case語句或跳出switch結(jié)構(gòu)。
             */
             
            // 版本1
            {
                int WeekOfDay;
                
                Console.Write("(Ver1)Enter a week of day: ");
                WeekOfDay = Convert.ToInt32(Console.ReadLine());
                
                switch (WeekOfDay)
                { 
                    case 1:
                        Console.WriteLine("Today is Monday.");
                        break;
                    case 2:
                        Console.WriteLine("Today is Tuesday.");
                        break;
                    case 3:
                        Console.WriteLine("Today is Wednesday.");
                        break;
                    case 4:
                        Console.WriteLine("Today is Thursday.");
                        break;
                    case 5:
                        Console.WriteLine("Today is Friday.");
                        break;
                    case 6:
                    case 7:
                        Console.WriteLine("Today is Weekend.");
                        break;
                    default:
                        return;
                }
            }
            
            // 版本2
            {
                int WeekOfDay;
                
                Console.Write("(Ver2)Enter a week of day: ");
                WeekOfDay = Convert.ToInt32(Console.ReadLine());
                
                switch (WeekOfDay)
                {
                    case 1:
                        Console.WriteLine("Today is Monday.");
                        break;
                    case 2:
                        Console.WriteLine("Today is Tuesday.");
                        break;
                    case 3:
                        Console.WriteLine("Today is Wednesday.");
                        break;
                    case 4:
                        Console.WriteLine("Today is Thursday.");
                        break;
                    case 5:
                        Console.WriteLine("Today is Friday.");
                        break;
                    case 6:
                        goto case 7;
                    case 7:
                        Console.WriteLine("Today is Weekend.");
                        return;
                    default:
                        goto End;
                }
                
            End:
                Console.WriteLine("The week of day is unknown.");
            }
            
            Console.ReadKey();
        }
    }
}


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI