代码改变世界

switch case

2016-06-20 16:57  天疯狂石  阅读(154)  评论(0编辑  收藏  举报

switch case

switch case 必须与 break 一同使用。

break是跳转语句。与switch case连用的时候是跳出最近的{}。

例1:

            Console.WriteLine("1.汉堡包");
            Console.WriteLine("2.薯条");
            Console.WriteLine("3.鸡块");
            Console.WriteLine("4.鸡腿");
            Console.WriteLine("5.鸡米花");

            Console.Write("请输入您的选择项目数字:");
            string a = Console.ReadLine();

            switch (a)//括号内是被判断的变量名称
            {
                case "1"://case后面的值是用来判断上面括号内的变量相不相等
                    Console.WriteLine("您选择的是汉堡包");
                    break;//break跳转语句,跳出最近的花括号
                case "2"://case与值之间有空格隔开     值后面是冒号
                    Console.WriteLine("您选择的是薯条");
                    break;
                case "3":
                    Console.WriteLine("您选择的是鸡块");
                    break;
                case "4":
                    Console.WriteLine("您选择的是鸡腿");
                    break;
                case "5":
                    Console.WriteLine("您选择的是鸡米花");
                    break;  

                default://意思是相当于else,表示以上的值都不对
                    Console.WriteLine("输入错误!");月有
                    break;//最后一个也需要跳出花括号
            }
            Console.ReadLine();

例2:分别输入月份、几号。输出是今年的第多少天?

        每年1 3 5 7 8 10 12月有31天

        今年2月有28天

        其他的4 6 9 11月有30天

            int m1 = 31, m2 = 28, m3 = 31, m4 = 30, m5 = 31, m6 = 30, m7 = 31, m8 = 31, m9 = 30, m10 = 31, m11 = 30;
            Console.WriteLine("输入月份:");
            int m = int.Parse(Console.ReadLine());
            Console.WriteLine("输入日期:");
            int d = int.Parse(Console.ReadLine());
            switch (m)
            {
                case 1:
                    Console.WriteLine("第" + d + "天");
                    break;

                case 2:
                    Console.WriteLine("第" + (m1 + d) + "天");
                    break;

                case 3:
                    Console.WriteLine("第" + (m1 + m2 + d) + "天");
                    break;

                case 4:
                    Console.WriteLine("第" + (m1 + m2 + m3 + d) + "天");
                    break;

                case 5:
                    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + d) + "天");
                    break;

                case 6:
                    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + d) + "天");
                    break;

                case 7:
                    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + d) + "天");
                    break;

                case 8:
                    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + d) + "天");
                    break;

                case 9:
                    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + d) + "天");
                    break;

                case 10:
                    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + d) + "天");
                    break;

                case 11:
                    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + d) + "天");
                    break;

                case 12:
                    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + m11 + d) + "天");
                    break;

                default:
                    Console.WriteLine("输入有误");
                    break;
            }
            Console.ReadLine();