20道C#练习题(一)1——10题

时间:2023-02-22 11:51:30

1.输入三个整数,xyz,最终以从小到大的方式输出。利用if嵌套。

             Console.Write("请输入x=");

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

            Console.Write("请输入y=");

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

            Console.Write("请输入z=");

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

            if (x < y && x < z)

            {

                Console.WriteLine(x);

                if(y<z)

                {

                    Console.WriteLine(y);

                    Console.WriteLine(z);

                }

                else

                {

                    Console.WriteLine(z);

                    Console.WriteLine(y);

                }

            }

            else if (y < x && y < z)

            {

                Console.WriteLine(y);

                if(x<z)

                {

                    Console.WriteLine(x);

                    Console.WriteLine(z);

                }

                else

                {

                    Console.WriteLine(z);

                    Console.WriteLine(x);

                }

 

            }

            else//z最小

            {

                Console.WriteLine(z);

                if(x<y)

                {

                    Console.WriteLine(x);

                    Console.WriteLine(y);

                }

                else

                {

                    Console.WriteLine(y);

                    Console.WriteLine(x);

                }

            }

2.输入三个整数,xyz,最终以从小到大的方式输出。利用中间变量。

          Console.Write("请输入x=");

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

            Console.Write("请输入y=");

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

            Console.Write("请输入z=");

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

            double zhong;

            if(x<y&&x<z)

            {

                if (y < z) { }

                else

                {

                    zhong = y; y = z; z = zhong;

                }

            }

            else if (y < x && y < z)

            {

                zhong = x; x = y; y = zhong;//x<y&&x<z

                if (y < z) { }

                else

                {

                    zhong = y; y = z; z = zhong;

                }

            }

            else //z最小

            {

                zhong = x; x = z; z = zhong;//x<y&&x<z

                if (y < z) { }

                else

                {

                    zhong = y; y = z; z = zhong;

                }

            }

            Console.WriteLine(x);

            Console.WriteLine(y);

            Console.WriteLine(z);

3.输入三个整数,xyz,最终以从小到大的方式输出。利用条件运算符。

           Console.Write("请输入x=");

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

            Console.Write("请输入y=");

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

            Console.Write("请输入z=");

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

            min = x > y ? (y > z ? z : y) : (x > z ? z : x);

            zhong = x > y ? (y > z ? y : (x>z?z:x)) : (x > z ? x : (y>z?z:y));         

            max = x > y ? (x > z ? x : z) : (y > z ? y : z);

            Console.WriteLine(min);

            Console.WriteLine(zhong);

            Console.WriteLine(max);

4.“现在几点了?”键盘键入小时数,判断是上午还是下午。打印出来现在是上午几点还是下午几点。利用条件运算符。

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

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

            string b=a>12?(a-12)+"pm":a+"am";

            Console.WriteLine("现在是"+b);

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

【结婚吧】【先买房子在结婚】【先赚钱再买房子再结婚】都没有【拜拜~~】

利用if嵌套做相亲过程。

            Console.WriteLine("你有房子吗?");

            string a = Console.ReadLine();

            if (a == "有")

            {

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

            }

            else

            {

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

                string b = Console.ReadLine();

                if (b == "有")

                {

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

                }

                else

                {

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

                    string c = Console.ReadLine();

                    if (c == "有")

                    {

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

                    }

                    else

                    {

                        Console.WriteLine("拜拜!");

                    }

                }

            }

 

6.输入年月日,看看格式是否正确。利用if嵌套。

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

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

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

            {

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

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

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

                {

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

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

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

                    {

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

                        {

                            Console.WriteLine("格式正确,你输入的是"+y+"年"+m+"月"+d+"日。");

                        }

                        else

                        {

                            Console.WriteLine("你输入日期格式有误。");

                        }

                    }

                    else if (m == 4 || m == 6 || m == 9 || m == 11)

                    {

                        if (d >= 1 && d <= 30)

                        {

                            Console.WriteLine("格式正确,你输入的是" + y + "年" + m + "月" + d + "日。");

                        }

                        else

                        {

                            Console.WriteLine("你输入日期格式有误。");

                        }

                    }

                    else//m==2

                    {

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

                        {

                            if (d >= 1 && d <= 29)

                            {

                                Console.WriteLine("格式正确,你输入的是" + y + "年" + m + "月" + d + "日。");

                            }

                            else

                            {

                                Console.WriteLine("你输入日期格式有误。");

                            }

                        }

                        else

                        {

                            if (d >= 1 && d <= 28)

                            {

                                Console.WriteLine("格式正确,你输入的是" + y + "年" + m + "月" + d + "日。");

                            }

                            else

                            {

                                Console.WriteLine("你输入日期格式有误。");

                            }

                        }

                    }

                }

                else

                {

                    Console.WriteLine("你输入的月份格式有误。");

                }

            }

            else

            {

                Console.WriteLine("你输入的年份格式有误。");

            }

 

7.输入年月日,看看格式是否正确。利用DateTime。

          Console.Write("请输入年月日(****/**/** **;**;**)");

            try

            {

                DateTime shijian = DateTime.Parse(Console.ReadLine());

                Console.WriteLine("格式正确,你输入的是:" + shijian);

            }

            catch

            {

                Console.WriteLine("你输入的格式有误。");

            }

 

8.做人机猜拳,剪刀石头布。利用switch case。

         int fenshu = 0;

            for (; ; )

            {

                Console.WriteLine("猜拳游戏:");

                Console.WriteLine("1、剪刀");

                Console.WriteLine("2、包袱");

                Console.WriteLine("3、锤");

                Console.WriteLine("4、结束");

                Console.Write("你要出的是:");

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

                Random ran = new Random();

                int n = ran.Next(1, 4);

                if (a >= 1 && a <= 3)

                {

                    switch (n)

                    {

                        case 1:

                            Console.WriteLine("电脑出:剪刀");

                            break;

                        case 2:

                            Console.WriteLine("电脑出:包袱");

                            break;

                        case 3:

                            Console.WriteLine("电脑出:锤");

                            break;

                    }

                    if(a-n==2||a-n==-1)

                    {

                        fenshu++;

                        Console.WriteLine("你赢了!");

                        Console.WriteLine("得分为:"+(fenshu));

                    }

                    else if(a-n==-2||a-n==1)

                    {

                        fenshu--;

                        Console.WriteLine("电脑赢了!");

                        Console.WriteLine("得分为:"+(fenshu));

                    }

                    else

                    {

                        Console.WriteLine("打平了!");

                        Console.WriteLine("得分为:" + (fenshu));

                    }

                    Console.WriteLine("请按回车键继续。");

                    Console.ReadLine();

                }

                else

                {

                    if (a == 4)

                    {

                        break;

                    }

                    Console.WriteLine("输入有误,请重新输入");

                }

            }

9.输入一个正整数,求1!+2!+3!+...+n!。利用for循环嵌套。

         Console.Write("请输入正整数n=");

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

            sum = 0;

            for (int i = 1; i <= n;i++ )

            {

                int sum1=1;

                for (int j = 1; j <= i;j++ )

                {

                    sum1 = sum1 * j;

                }

                sum = sum + sum1;

            }

            Console.WriteLine("阶乘和:" + sum);

10.找出100以内与7有关的数并打印,并求出他们的和。利用for循环+if。

         int sum = 0;

            for (int i = 0;i<=100;i++ )

            {              

                if(i%7==0||i%10==7||i/10==7)

                {

                    Console.WriteLine(i);

                    sum = sum + i;

                }              

            }

            Console.WriteLine("总和为:"+(sum));