• 传说中的二十道题(一)


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

                Console.Write("请输入x=");
                int x = int.Parse(Console.ReadLine());
                Console.Write("请输入y=");
                int y = int.Parse(Console.ReadLine());
                Console.Write("请输入z=");
                int z = int.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);
                    }
                }
                if (y < x && y < z)
                {
                    Console.WriteLine(y);
                    if (x < z)
                    {
                        Console.WriteLine(x);
                        Console.WriteLine(z);
                    }
                    else
                    {
                        Console.WriteLine(z);
                        Console.WriteLine(x);
                    }
                }
                if (z < x && z < y)
                {
                    Console.WriteLine(z);
                    if (x < y)
                    {
                        Console.WriteLine(x);
                        Console.WriteLine(y);
                    }
                    else
                    {
                        Console.WriteLine(y);
                        Console.WriteLine(x);
                    }
                }
                Console.ReadLine();

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

                Console.Write("请输入x=");
                int x = int.Parse(Console.ReadLine());
                Console.Write("请输入y=");
                int y = int.Parse(Console.ReadLine());
                Console.Write("请输入z=");
                int z = int.Parse(Console.ReadLine());
                int zhong;
                if (x < y && x < z)
                {
                    if (y < z)
                    {
                    }
                    else
                    {
                        zhong = y;
                        y = z;
                        z = zhong;
                    }
                }
                else if (y < x && y < z)
                {
                    zhong = y;
                    y = x;
                    x = zhong;
                    if (y < z)
                    {
                    }
                    else
                    {
                        zhong = y;
                        y = z;
                        z = zhong;
                    }
                }
                else
                {
                    zhong = x;
                    x = z;
                    z = zhong;
                    if (y < z)
                    {
                    }
                    else
                    {
                        zhong = y;
                        y = z;
                        z = zhong;
                    }
                }
                Console.WriteLine(x);
                Console.WriteLine(y);
                Console.WriteLine(z);
                Console.ReadLine();

    另一种简单的方法

                Console.Write("请输入x=");
                int x = int.Parse(Console.ReadLine());
                Console.Write("请输入y=");
                int y = int.Parse(Console.ReadLine());
                Console.Write("请输入z=");
                int z = int.Parse(Console.ReadLine());
                int zhong;
                if (x > y)
                {
                    zhong = x;
                    x = y;
                    y = zhong;
                }
                if (x > z)
                {
                    zhong = x;
                    x = z;
                    z = zhong;
                }
                if (y > z)
                {
                    zhong = y;
                    y = z;
                    z = zhong;
                }
                Console.WriteLine(x);
                Console.WriteLine(y);
                Console.WriteLine(z);

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

                Console.Write("请输入x=");
                int x = int.Parse(Console.ReadLine());
                Console.Write("请输入y=");
                int y = int.Parse(Console.ReadLine());
                Console.Write("请输入z=");
                int z = int.Parse(Console.ReadLine());
                if (x < y && x < z)
                {
                    string a = y < z ? "" + y : "" + z;
                    string b = y > z ? "" + y : "" + z;
                    Console.WriteLine(x);
                    Console.WriteLine(a);
                    Console.WriteLine(b);
                }
                if (y < x && y < z)
                {
                    string c = x < z ? "" + x : "" + z;
                    string d = x > z ? "" + x : "" + z;
                    Console.WriteLine(y);
                    Console.WriteLine(c);
                    Console.WriteLine(d);
                }
                if (z < x && z < y)
                {
                    string e = x < y ? "" + x : "" + y;
                    string f = x > y ? "" + x : "" + y;
                    Console.WriteLine(z);
                    Console.WriteLine(e);
                    Console.WriteLine(f);
                }
                Console.ReadLine();

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

                Console.Write("现在几点了");
                int shi = int.Parse(Console.ReadLine());
                string m = shi > 12 ? "现在是pm" + (shi - 12) + "" : "现在是am";
                Console.WriteLine(m);
                Console.ReadLine();

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

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

    利用if嵌套做相亲过程

                Console.Write("你有房子吗?");
                string a = Console.ReadLine();
                if (a == "")
                {
                    Console.WriteLine("我们结婚吧");
                }
                else
                {
                    Console.Write("你有钱吗");
                    string b = Console.ReadLine();
                    if (b == "")
                    {
                        Console.WriteLine("先买房子再结婚");
                    }
                    else
                    {
                        Console.Write("你有能力吗");
                        string c = Console.ReadLine();
                        if (c == "")
                        {
                            Console.WriteLine("先赚钱再买房子再结婚");
                        }
                        else
                        {
                            Console.WriteLine("拜拜");
                        }
                    }
                }
                Console.ReadLine();

    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 > 0 && m <= 12)
                    {
                        Console.Write("请输入日期");
                        int d = int.Parse(Console.ReadLine());
                        if (d > 0 && d <= 31)
                        {
                            if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
                            {
                                Console.WriteLine("格式正确是" + y + "" + m + "" + d + "");
                            }
                            else if (m == 4 || m == 6 || m == 9 || m == 11)
                            {
                                if (d <= 30)
                                {
                                    Console.WriteLine("格式正确是" + y + "" + m + "" + d + "");
                                }
                                else//d>30
                                {
                                    Console.WriteLine("日期有误");
                                }
                            }
                            else//2月
                            {
                                if (d <= 28)
                                {
                                    Console.WriteLine("格式正确是" + y + "" + m + "" + d + "");
                                }
                                else//29,30,31
                                {
                                    if (d == 29)
                                    {
                                        if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
                                        {
                                            Console.WriteLine("格式正确是" + y + "" + m + "" + d + "");
                                        }
                                        else
                                        {
                                            Console.WriteLine("日期有误");
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine("日期有误");
                                    }
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine("日期有误");
                        }
                    }
                    else
                    {
                        Console.WriteLine("月份有误");
                    }
                }
                else
                {
                    Console.WriteLine("年份有误");
                }
                Console.ReadLine();

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

                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 > 0 && a < 4)
                    {
                        switch (n)
                        {
                            case 1:
                                Console.WriteLine("电脑出拳:剪子");
                                break;
                            case 2:
                                Console.WriteLine("电脑出拳:包袱");
                                break;
                            case 3:
                                Console.WriteLine("电脑出拳:锤");
                                break;
                        }
                    }
                    if (a >= 1 && a <= 3)
                    {
                        if ((a - n) == -1 || (a - n) == 2)
                        {
    
                            Console.WriteLine("您赢了!请按回车继续!");
                        }
                        else if ((a - n) == -2 || (a - n) == 1)
                        {
    
                            Console.WriteLine("您输了!请按回车继续!");
                        }
                        else
                        {
                            Console.WriteLine("平局!请按回车继续!");
                        }
                        Console.ReadLine();
                    }
                    else
                    {
                        if (a == 4)
                        {
                            break;
                        }
                        Console.WriteLine("输入有误,请按回车继续!");
                        Console.ReadLine();
                    }
                }

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

                Console.Write("请输入一个正整数");
                int a = int.Parse(Console.ReadLine());
                int sum = 0;
                for (int i = 1; i <= a; i++)
                {
                    int sum1 = 1;
                    for (int j = 1; j <= i; j++)
                    {
                        sum1 *= j;
                    }
                    sum += sum1;
                }
                Console.WriteLine(sum);
                Console.ReadLine();

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

                int sum = 0;
                for (int i = 1; i <= 100; i++)
                {
                    if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7)
                    {
                        Console.WriteLine(i);
                    }
                    sum += i;
                }
                Console.WriteLine(sum);
                Console.ReadLine();
  • 相关阅读:
    SpringBoot学习之配置Redis
    安全测试12使用nmap工具识别系统指纹信息
    安全测试11nmap扫描开放的端口
    安全测试17渗透攻击Mysql数据库服务
    安全测试18渗透攻击Tomcat服务
    安全测试16漏洞扫描工具Nikto详细使用教程
    实用且靠谱的18个免费引流推广方法
    安全测试15Maltego详细使用教程
    安全测试14ARP侦查工具Netdiscover
    统计本机tcp连接情况分离排查问题
  • 原文地址:https://www.cnblogs.com/Duriyya/p/5292199.html
Copyright © 2020-2023  润新知