十四、流程控制之switch语句-成都快上网建站

十四、流程控制之switch语句

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 ()
             *  {
             *      case :
             *           == >
             *          break;
             *      case :
             *           == >
             *          break;
             *      ...
             *      case :
             *           == >
             *          break;
             *      default:
             *           != >
             *          break;
             *  }
             *  
             * 解释说明:
             *  1. 中的值与每个值(在case语句中指定),如果有一个匹配,
             *     就执行为该匹配提供的语句。如果没有匹配,就执行default部分中的代码。
             *  2. 执行完每个部分中的代码后,还需要另一个语句break。
             *  3. 在执行完一个case块后,再执行第二个case语句是非法的。
             *  4. 每个都必须是一个常数值(字面值或常量)。
             *  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();
        }
    }
}

网站名称:十四、流程控制之switch语句
文章地址:http://kswjz.com/article/jsgjdp.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流