选择语言之switch case

2018-06-22 07:05:41来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

程序语言-选择语言之switch   case

多选一,类似if    else if  else if  else

模版:

Switch(选择条件)

Case(条件一)//相当于if

Console.Writeline(“******”);

Break;

Case(2)

Console.Writeline(“******”);

Break;

…………

…………

default//相当于else

Console.Writeline(“******”);

Break;

输入月份日期,输出第几天

Console.Write("请输入月份:");

            int a = int.Parse(Console.ReadLine());

            Console.Write("请输入日期:");

            int d = int.Parse(Console.ReadLine());

            int m1 = 31, m2 = 28, m3 = 31, m4 = 30, m5 = 31, m6 = 30, m7 = 31, m8 = 31, m9 = 30, m10 = 31, m11 = 30;

            switch(a)

            {

                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();

 

 

 

汇总十个小题目练习:

 

            Console.WriteLine("1.键盘键入三个数,找出来最大的数。");

            Console.WriteLine("2.“现在几点了?”键盘键入小时数,判断是上午还是下午。打印出来现在是上午几点还是下午几点");

            Console.WriteLine("3.输入一个小于等于100的整数,判断:?是小于10的?两位数?是100");

            Console.WriteLine("4.输入学生姓名,输入考试成绩,若是100,【恭喜你***,满分通过!】若是大于等于80小于100,【**,你很优秀,继续保持!】若是大于等于60小于80,【**成绩良好】大于等于50小于60,【**就差一点点,下次一定要至少及格!】");

            Console.WriteLine("5.输入年份,判断是否是闰年");

            Console.WriteLine("6.有一组函数:y = x (x<1);y = 2x -1  (1<=x<10); y = 3x-11  (x>=10)。括号内是x的满足条件。实现功能,随意输入一个x值,输出y的值。");

            Console.WriteLine("7.相亲过程:你有房子么?你有钱么?你有能力么?【结婚吧】【先买房子在结婚】【先赚钱再买房子再结婚】都没有【拜拜~~】利用if嵌套做相亲过程");

            Console.WriteLine("8.输入年月日,看看格式是否正确");

            Console.WriteLine("9.方程ax2+bx+c=0;一元二次方程。求根△=b2-4ac;若△<0方程无实根若△>0,方程有两个不相同的实根x1   x2若△=0,方程有两个相同的实根");

            Console.WriteLine("10.标准体重:");

 

            Console.Write("请输入题目编号:");

            int a = int.Parse(Console.ReadLine());

            switch (a)

            {

                case 1:

                    Console.Write("请输入数字1:");

                    int x = int.Parse(Console.ReadLine());

                    Console.Write("请输入数字2:");

                    int y = int.Parse(Console.ReadLine());

                    Console.Write("请输入数字3:");

                    int z = int.Parse(Console.ReadLine());

                    if (x < y)

                    {

                        if (y < z)

                        {

                            Console.WriteLine("最大的数字式" + z);

                        }

                        else

                        {

                            Console.WriteLine("最大的数字式" + y);

                        }

 

                    }

                    else

                    {

                        if (x > z)

                        {

                            Console.WriteLine("最大的数字式" + x);

                        }

                        else if (x < z)

                        {

                            Console.WriteLine("最大的数字式" + z);

                        }

                        else

                        {

                            Console.WriteLine("最大的数字是:" + x);

                        }

                    }

 

                    break;

                case 2:

                    Console.Write("现在几点了?");

                    double t = double.Parse(Console.ReadLine());

                    if (t > 12 && t < 24)

                    {

                        Console.WriteLine("现在的时间是:" + (t - 12) + "pm");

                    }

                    else if (t <= 0)

                    {

                        Console.WriteLine("现在的时间是:" + t + "am");

                    }

                    else

                    {

                        Console.WriteLine("输入错误!");

                    }

                    break;

                case 3:

                    Console.Write("请输入一位小于100的正数:");

                    int s = int.Parse(Console.ReadLine());

                    if (s < 101 && s >= 0)

                    {

                        if (s < 10)

                        {

                            Console.WriteLine("小于10!");

                        }

                        else if (s == 100)

                        {

                            Console.WriteLine("数字是100!");

 

                        }

                        else

                        {

                            Console.WriteLine("数字是两位数!");

                        }

                    }

                    else

                    {

                        Console.WriteLine("输入有误!");

                    }

 

                    break;

                case 4:

                    Console.Write("请输入姓名:");

                    string name = Console.ReadLine();

                    Console.Write("请输入成绩");

                    double h = double.Parse(Console.ReadLine());

                    if (h <= 100 && h >= 0)

                    {

                        if (h == 100)

                        {

                            Console.Write("恭喜你" + name + ",满分通过!");

                        }

                        else if (h < 100 && h <= 80)

                        {

                            Console.Write("" + name + ",你很优秀,继续保持!");

                        }

                        else if (h < 80 && h >= 60)

                        {

                            Console.Write("" + name + ",成绩良好");

                        }

                        else if (h >= 50 && h < 60)

                        {

                            Console.Write("" + name + ",就差一点点,下次一定要至少及格!");

                        }

                        else

                        {

                            Console.Write("" + name + "笨蛋!");

                        }

                    }

                    else

                    {

                        Console.WriteLine("输入有误!");

                    }

                    break;

                case 5:

                    Console.Write("请输入年份:");

                    int n = int.Parse(Console.ReadLine());

                    if (n <= 9999 && n >= 0)

                    {

                        if ((n % 4 == 0 && n % 10 != 0) || n % 400 == 0)

                        {

                            Console.Write("是闰年");

                        }

                        else

                        {

                            Console.Write("不是闰年");

                        }

                    }

                    else

                    {

                        Console.WriteLine("输入有误!");

                    }

 

                    break;

                case 6:

                    Console.Write("请输入一个x的值");

                    int xx = int.Parse(Console.ReadLine());

 

                    if (xx >= 10)

                    {

                        int yy = 3 * xx - 11;

                        Console.WriteLine("y的数值是" + yy);

                    }

                    else

                    {

                        if (xx < 1)

                        {

                            Console.WriteLine("y的数值是" + xx);

                        }

                        else

                        {

                            int yy = 2 * xx - 1;

                            Console.WriteLine("y的数值是" + yy);

                        }

 

                    }

 

                    break;

                case 7:

                    Console.Write("你有房子么?");

                    string c = Console.ReadLine();

                    if (c == "有")

                    {

                        Console.WriteLine("结婚吧");

                    }

                    else

                    {

                        Console.Write("你有钱么?");

                        string d = Console.ReadLine();

                        if (d == "有")

                        {

                            Console.WriteLine("先买房子在结婚?");

                        }

                        else

                        {

                            Console.Write("你有能力么?");

                            string e = Console.ReadLine();

                            if (e == "有")

                            {

                                Console.WriteLine("先挣钱再买房子在结婚?");

                            }

                            else

                            {

                                Console.WriteLine("拜拜!");

                            }

                        }

                    }

                    break;

                case 8:

                    Console.Write("请输入年份:");

                    int ye = int.Parse(Console.ReadLine());

                    Console.Write("请输入月份:");

                    int mo = int.Parse(Console.ReadLine());

                    Console.Write("请输入日:");

                    int da = int.Parse(Console.ReadLine());

                    if (ye <= 9999 && ye >= 0)

                    {

                        if (mo <= 12 && mo >= 1)

                        {

                            if (da <= 31 && da >= 0)

                            {

                                if (mo == 1 || mo == 3 || mo == 5 || mo == 7 || mo == 8 || mo == 10 || mo == 12)

                                {

                                    Console.WriteLine("您输入的日期是:" + ye + mo + da);

                                }

                                else if ((mo == 4 || mo == 6 || mo == 9 || mo == 11) && ye < 31)

                                {

                                    Console.WriteLine("您输入的日期是:" + ye + mo + da);

                                }

                                else if ((ye % 4 == 0 && ye % 10 != 0) || ye % 400 == 0 && da <= 29)

                                {

                                    Console.WriteLine("您输入的日期是:" + ye + mo + da);

                                }

                                else if ((ye % 4 != 0 && ye % 10 == 0) || ye % 400 != 0 && da < 29)

                                {

                                    Console.WriteLine("您输入的日期是:" + ye + mo + da);

                                }

                                else

                                {

                                    Console.WriteLine("输入错误!");

                                }

                            }

                            else

                            {

                                Console.WriteLine("输入错误!");

                            }

                        }

                        else

                        {

                            Console.WriteLine("输入错误!");

                        }

                    }

                    else

                    {

                        Console.WriteLine("输入错误!");

                    }

 

                    break;

                case 9:

                    Console.WriteLine("求方乘ax*x+bx+c=0的解");

                    Console.Write("请输入a:");

                    double qq = double.Parse(Console.ReadLine());

                    Console.Write("请输入b:");

                    double ww = double.Parse(Console.ReadLine());

                    Console.Write("请输入c:");

                    double ee = double.Parse(Console.ReadLine());

                    double de = ww * ww - 4 * qq * ee;

                    if (de < 0)

                    {

                        Console.WriteLine("方乘无解");

                    }

                    else

                    {

                        if (de == 0)

                        {

                            double x1 = (-ww + Math.Sqrt(de)) / (2 * a);

                            Console.WriteLine("方乘有一个解,x1=x2=" + x1);

                        }

                        else

                        {

                            double x1 = (-ww + Math.Sqrt(de)) / (2 * a);

                            double x2 = (-ww - Math.Sqrt(de)) / (2 * a);

                            Console.WriteLine("方乘有2个解,x1=" + x1, "x2=" + x2);

                        }

                    }

 

 

                    break;

                case 10:

                    Console.Write("请输入性别:");

                    string sex = Console.ReadLine();

                    Console.Write("请输入身高");

                    double hi = double.Parse(Console.ReadLine());

                    Console.Write("请输入体重");

                    double w = double.Parse(Console.ReadLine());

                    double k1 = w - hi + 100;

                    double k2 = w - hi + 110;

                    if (sex == "男")

                    {

                        if (k1 <= 3 && k1 >= -3)

                        {

                            Console.WriteLine("您的体重是正常的!");

                        }

                        else if (k1 > 3)

                        {

                            Console.WriteLine("您需要减肥了!");

                        }

                        else

                        {

                            Console.WriteLine("您需要增肥了!");

                        }

 

                    }

                    else if (sex == "女")

                    {

                        if (k2 >= -3 && k2 <= 3)

                        {

                            Console.WriteLine("您的体重是正常的!");

                        }

                        else if (k2 > 3)

                        {

                            Console.WriteLine("您需要减肥了!");

                        }

                        else

                        {

                            Console.WriteLine("您需要增肥了!");

                        }

                    }

                    else

                    {

                        Console.WriteLine("输入错误!");

                    }

                    break;

            }

            Console.ReadLine();

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:HTTP文件断点续传的原理

下一篇:扩展lamda表达中distinct按照字段去除重复