成都创新互联网站制作重庆分公司

十四、流程控制之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://cxhlcq.com/article/jsgjdp.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部