你的位置:首页 > ASP.net教程

[ASP.net教程]C#语句1:选择语句二(switch break)


(二)switch case

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

                       

注: ●若case后面接收的是字符串类型(string),需要:case “1”:

     ●default相当于else。

     ●switch case 类型一定可以转换成 if else 类型,相反,则不一定。

例题: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;  //最后一个也需要跳出花括号

            }

2、分别输入月份   几号    输出是今年的第多少天。(每年的1 3 5 7 8 10 12月是31天,今年的2月是28天,其他的4 6 9 11是30天)

            int a1 = 31, a2 = 28, a3 = 31, a4 = 30, a5 = 31, a6 = 30, a7 = 31, a8 = 31, a9 = 30, a10 = 31, a11 = 30, a12 = 31;

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

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

            Console.Write("请输入几号");

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

            switch (a)

            {

                case  1:

                    Console.WriteLine("第"+b+"天");

                    break;

                case 2:

                    Console.WriteLine("第" + (a1+b) + "天");

                    break;

                case 3:

                    Console.WriteLine("第" + (a1+a2+b) + "天");

                    break;

                case 4:

                    Console.WriteLine("第" + (a1 + a2 + a3 + b) + "天");

                    break;

                case 5:

                    Console.WriteLine("第" + (a1 + a2 + a3 + a4+ b) + "天");

                    break;

                case 6:

                    Console.WriteLine("第" + (a1 + a2 + a3 + a4 + a5 + b) + "天");

                    break;

                case 7:

                    Console.WriteLine("第" + (a1 + a2 + a3 + a4 + a5 + a6 + b) + "天");

                    break;

                case 8:

                    Console.WriteLine("第" + (a1 + a2 + a3 + a4 + a5 + a6 + a7 + b) + "天");

                    break;

                case 9:

                    Console.WriteLine("第" + (a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + b) + "天");

                    break;

                case 10:

                    Console.WriteLine("第" + (a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + b) + "天");

                    break;

                case 11:

                    Console.WriteLine("第" + (a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10 + b) + "天");

                    break;

                case 12:

                    Console.WriteLine("第" + (a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10 + a11 + b) + "天");

                    break;

                default :

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

                    break;

            }

            Console.ReadLine();

3、将10个题目用一个程序运行出来:

①键盘键入三个数,找出来最大的数

②“现在几点了?”键盘键入小时数,判断是上午还是下午。打印出来现在是上午几点还是下午几点

③输入一个小于等于100的整数,判断:是小于10的,两位数,是100。

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

⑤输入年份判断是否为闰年

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

⑦相亲过程:你有房子么?你有钱么?你有能力么?

【结婚吧】【先买房子在结婚】【先赚钱再买房子再结婚】都没有【拜拜~~】利用if嵌套做相亲过程

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

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

⑩标准体重:男士体重=身高-100±3,女士体重=身高-110±3。

程序:      Console.WriteLine("1、三个数比大小");

            Console.WriteLine("2、上午、下午几点");

            Console.WriteLine("3、小于100整数");

            Console.WriteLine("4、考试成绩情况");

            Console.WriteLine("5、判断闰年");

            Console.WriteLine("6、知x求y");

            Console.WriteLine("7、相亲");

            Console.WriteLine("8、年、月、日正确格式");

            Console.WriteLine("9、一元二次方程");

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

            Console.WriteLine("请输入你想求解的题目的编号");

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

            switch (m)

            {

                case 1:

                    Console.Write("请输入一个数:r=");

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

                    Console.Write("请输入一个数:s=");

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

                    Console.Write("请输入一个数:t=");

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

                    if (r < s && r < t)

                    {

                        if (s < t)

                        {

                            Console.WriteLine("三个数有小到大排序是:{0}<{1}<{2}", r, s, t);

                        }

                        else

                        {

                            Console.WriteLine("三个数有小到大排序是:{0}<{1}<{2}", r, t, s);

                        }

                    }

                    else if (s < r && s < t)

                    {

                        if (r < t)

                        {

                            Console.WriteLine("三个数有小到大排序是:{0}<{1}<{2}", s, r, t);

                        }

                        else

                        {

                            Console.WriteLine("三个数有小到大排序是:{0}<{1}<{2}", s, t, r);

                        }

                    }

                    else if (t < r && t < s)

                    {

                        if (r < s)

                        {

                            Console.WriteLine("三个数有小到大排序是:{0}<{1}<{2}", t, r, s);

                        }

                        else

                        {

                            Console.WriteLine("三个数有小到大排序是:{0}<{1}<{2}", t, s, r);

                        }

                    }

                    else

                    {

                        Console.WriteLine("输入的数有错误");

                    }

                    break;

                case 2:

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

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

                    if (e >= 0 && e <= 24)

                    {

                        if (e <= 12)

                        {

                            Console.WriteLine("现在是上午" + e + "点");

                        }

                        else

                        {

                            int f = e - 12;

                            Console.WriteLine("现在是下午{0}点", f);

                        }

                    }

                    else

                    {

                        Console.WriteLine("您输入的时间点有误");

                    }

                    break;

                case 3:

                    Console.Write("请输入一个小于等于100的整数g=");

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

                    if (g >= 10 && g < 100)

                    {

                        Console.WriteLine("您输入的整数是两位数");

                    }

                    else if (g < 10)

                    {

                        Console.WriteLine("您输入的整数是小于10的");

                    }

                    else

                    {

                        Console.WriteLine("您输入的整数是100");

                    }

                    break;

                case 4:

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

                    string h = Console.ReadLine();

                    Console.Write("输入考试成绩:");

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

                    if (i == 100)

                    {

                        Console.WriteLine("恭喜你***,满分通过!");

                    }

                    else if (i >= 80 && i < 100)

                    {

                        Console.WriteLine("**,你很优秀,继续保持!");

                    }

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

                    {

                        Console.WriteLine("**成绩良好");

                    }

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

                    {

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

                    }

                    else

                    {

                        Console.WriteLine("成绩输入错误");

                    }

                    break;

                case 5:

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

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

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

                    {

                        if ((j % 4 == 0 && j % 100 != 0) || j % 400 == 0)

                        {

                            Console.WriteLine("您输入的年份是闰年!");

                        }

                    }

                    else

                    {

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

                    }

                    break;

                case 6:

                    Console.Write("请输入一个数x:");

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

                    if (k < 1)

                    {

                        Console.WriteLine("y=" + k);

                    }

                    else if (k >= 1 && k < 10)

                    {

                        Console.WriteLine("y=" + (2 * k - 1));

                    }

                    else

                    {

                        Console.WriteLine("y=" + (3 * k - 11));

                    }

                    break;

                case 7:

                    Console.Write("你有房子吗");

                    string home = Console.ReadLine();

                    if (home == "有")

                    {

                        Console.WriteLine("结婚吧!");

                    }

                    else

                    {

                        Console.WriteLine("你有钱吗");

                        string money = Console.ReadLine();

                        if (money == "有")

                        {

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

                        }

                        else

                        {

                            Console.WriteLine("你有能力吗");

                            string ability = Console.ReadLine();

                            if (ability == "有")

                            {

                                Console.WriteLine("先赚钱再房子再结婚");

                            }

                            else

                            {

                                Console.WriteLine("拜拜!");

                            }

                        }

                    }

                    break;

                case 8:

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

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

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

                    {

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

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

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

                        {

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

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

                            if (day >= 1 && day <= 31)

                            {

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

                                {

                                    Console.WriteLine("您输入的日期格式正确!日期是:{0}-{1}-{2}", year, month, day);

                                }

                                else

                                {

                                    if (month == 4 || month == 6 || month == 9 || month == 11)

                                    {

                                        if (day <= 30)

                                        {

                                            Console.WriteLine("您输入的日期格式正确!日期是:{0}-{1}-{2}", year, month, day);

                                        }

                                        else

                                        {

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

                                        }

                                    }

                                    else

                                    {

                                        if (day <= 28)

                                        {

                                            Console.WriteLine("您输入的日期格式正确!日期是:{0}-{1}-{2}", year, month, day);

                                        }

                                        else

                                        {

                                            if (day == 29)

                                            {

                                                if ((day % 4 == 0 && day % 100 != 0) || day % 400 == 0)

                                                {

                                                    Console.WriteLine("您输入的日期格式正确!日期是:{0}-{1}-{2}", year, month, day);

                                                }

                                                else

                                                {

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

                                                }

                                            }

                                            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 a = double.Parse(Console.ReadLine());

                    Console.Write("输入b=");

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

                    Console.Write("输入c=");

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

                    double de = b * b - 4 * a * c;

                    if (a == 0)

                    {

                        Console.WriteLine("方程不是一元二次方程");

                    }

                    else

                    {

                        Console.WriteLine("方程是一元二次方程");

                        if (de >= 0)

                        {

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

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

                            if (de > 0)

                            {

                                Console.Write("方程有两个不同的实数根:");

                                Console.WriteLine("x1=" + x1, "x2=" + x2);

                            }

                            else

                            {

                                Console.Write("方程有两个相同的实数根:");

                                Console.WriteLine("x1=x2=" + x1);

                            }

                        }

                        else

                        {

                            Console.WriteLine("方程没有实数根");

                        }

                    }

                    break;

                case 10:

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

                    string x = Console.ReadLine();

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

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

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

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

                    if (x == "男")

                    {

                        double d = y - z + 100;

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

                        {

                            Console.WriteLine("您的体重是标准的!");

                        }

                        else if (d > 3)

                        {

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

                        }

                        else

                        {

                            Console.WriteLine("您需要增加营养!");

                        }

                    }

                    else if (x == "女")

                    {

                        double d = y - z + 110;

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

                        {

                            Console.WriteLine("您的体重是标准的!");

                        }

                        else if (d > 3)

                        {

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

                        }

                        else

                        {

                            Console.WriteLine("您需要增加营养!");

                        }

 

                    }

                    else

                    {

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

                    }

                    break;

                default:

                    Console.WriteLine("您输入的编号有误");

                    break;

            }

            Console.ReadLine();