首页 > 其他 > 详细

switch case

时间:2016-03-12 09:10:40      阅读:139      评论:0      收藏:0      [点我收藏+]
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":
                    Console.WriteLine("您选择的是汉堡包!");
                    break;
                case"2":
                    Console.WriteLine("您选择的是薯条!");
                    break;
                case"3":
                    Console.WriteLine("您选择的是鸡块!");
                    break;
                case "4":
                    Console.WriteLine("您选择的是鸡腿!");
                    break;
                case "5":
                    Console.WriteLine("您选择的是鸡米花!");
                    break;
                default:
                    Console.WriteLine("输入有误!");
                    break;
}
技术分享

switch case就是一种选择语句

逻辑性思维题:

例:

判断是不是闰年,普通年份,是4的倍数但是不能是100的倍数
世纪年需要是400的倍数

输入一个年份,判断是不是闰年

Console.Write("请输入一个年份:");
int year = int.Parse(Console.ReadLine());
if (year >= 0 && year <= 9999)

技术分享
            Console.Write("请输入一个年份:");
            int year = int.Parse(Console.ReadLine());
            if (year >= 0 && year <= 9999)
            {
                if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
                {
                    Console.WriteLine("您输入的年份是闰年!");
                }
                else
                {
                    Console.WriteLine("您输入的年份是平年!");
                }
            }
            else
            {
                Console.WriteLine("您所输入的年份不正确。");
            }
技术分享

这个例题还算好,只要知道闰年是怎么回事就行了。

例:

输入年月日,看看格式是否正确

技术分享

做了三遍依旧有错误。

技术分享
Console.Write("请输入一个年份:");
            int year = int.Parse(Console.ReadLine());
            if (year >= 0 && year <= 9999)
            {
                Console.Write("请输入月份:");
                int m = int.Parse(Console.ReadLine());
                if (m >= 1 && m <= 12)
                {
                    Console.Write("请输入日期:");
                    int day = int.Parse(Console.ReadLine());
                    if (day >= 1 && day <= 31)
                    {
                        if (m == 1 || m == 3 || m == 5 || m == 7 || m == 10 || m == 12)
                        {
                            Console.WriteLine("您输入的日期格式正确,"+year+""+m+""+day);
                        }
                        if(m==4||m==6||m==9||m==11)
                        {
                            if(day<=30)
                            {
                                 Console.WriteLine("您输入的日期格式正确,"+year+""+m+""+day);
                            }
                            else
                            {
                                Console.WriteLine("您输入的日期有误!");
                            }
                        }
                        else//剩下的二月份
                        {
                            if(year%4==0&&year%100!=0||year%400==0)//闰年的情况
                            {
                                if(day<=29)
                                {
                                     Console.WriteLine("您输入的日期格式正确,"+year+""+m+""+day);
                                }
                                else
                                {
                                    Console.WriteLine("您输入的日期格式有误!");
                                }
                            }
                            else//平年
                            {
                                 if(day<=28)
                                {
                                     Console.WriteLine("您输入的日期格式正确,"+year+""+m+""+day);
                                }
                                else
                                {
                                    Console.WriteLine("您输入的日期格式有误!");
                                }
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("您输入的日期有误!");
                    }
                }
                else
                {
                    Console.WriteLine("您输入的月份各式有误!");
                }
                     
            }
                 
            else
            {
                Console.WriteLine("您输入的年份格式不正确!");
            }

            Console.ReadLine();

switch case

原文:http://www.cnblogs.com/dianfu123/p/5267850.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!