温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

十四、流程控制之switch语句

发布时间:2020-07-06 13:31:12 来源:网络 阅读:501 作者: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语句可以一次将测试变量与多个值进行比较,而不是仅测试一个条件。
             * 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>中的值与每个<comparisonVarX>值(在case语句中指定),如果有一个匹配,
             *     就执行为该匹配提供的语句。如果没有匹配,就执行default部分中的代码。
             *  2. 执行完每个部分中的代码后,还需要另一个语句break。
             *  3. 在执行完一个case块后,再执行第二个case语句是非法的。
             *  4. 每个<comarisonVarX>都必须是一个常数值(字面值或常量)。
             *  5. switch语句对case语句的数量没有限制。
             *  
             * 可以使用三种方式防止程序流程从一个case语句转到下一个case语句:
             * 1. break语句将中断switch语句的执行,而执行该结构后面的语句。
             * 2. return语句将中断当前函数的运行,而不是仅中断switch结构的执行。
             * 3. 可以使用goto语句跳转到其他case语句或跳出switch结构。
             */
             
            // 版本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问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI