zoukankan      html  css  js  c++  java
  • 寒假作业(2)

    6.输入年月日,看看格式是否正确。利用if嵌套。
                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("您输入的日期格式正确,输入的日期时间是:"+year+"-"+month+"-"+day);
                            }
                            else if (month == 4 || month == 6 || month == 9 || month == 11)
                            {
                                if (day <= 30)
                                {
                                    Console.WriteLine("您输入的日期格式正确,输入的日期时间是:" + year + "-" + month + "-" + day);
                                }
                                else
                                {
                                    Console.WriteLine("您输入的日期格式错误!");
                                }
                            }
                            else//2
                            {
                                if (day <= 28)
                                {
                                    Console.WriteLine("您输入的日期格式正确,输入的日期时间是:" + year + "-" + month + "-" + day);
                                }
                                else if (day == 29)
                                {
                                    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
                                    {
                                        Console.WriteLine("您输入的日期格式正确,输入的日期时间是:" + year + "-" + month + "-" + day);
                                    }
                                    else
                                    {
                                        Console.WriteLine("您输入的日期格式错误!");
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("您输入的日期格式错误!");
                                }
                            }
                        }
                        else
                        {
                        Console.WriteLine("您输入的日期格式错误!");
                        }
                    }
                    else
                    {
                        Console.WriteLine("您输入的日期格式错误!");
                    }
                }
                else
                {
                    Console.WriteLine("您输入的日期格式错误!");
                }
                Console.ReadLine();


    7.输入年月日,看看格式是否正确。利用DateTime。
                2013-2-2          2013/2/2
                try   catch  finally
                Console.WriteLine("请输入日期:");
                try
                {
                    DateTime time1 = DateTime.Parse(Console.ReadLine());
                    Console.WriteLine("您输入的格式正确!");
                }
                catch
                {
                    Console.WriteLine("您输入的格式不正确!");
                }
                finally
                {
                    Console.WriteLine("感谢您的使用!");
                }
                Console.ReadLine();


    8.做人机猜拳,剪刀石头布。利用switch case。
                随机,Random        .next(1,3)
                Random a = new Random();
                一局定胜负
                Console.WriteLine("石头剪刀布出什么");
                string a = Console.ReadLine();
                int b = 0;
                 switch(a)
                {
                    case "石头":
                        b = 1;
                        break;
                    case "剪刀":
                        b = 2;
                        break;
                    case "布":
                        b = 3;
                        break;
                }

                Random x = new Random();
                int y = 0;
                int z = 0;
                y = x.Next(1,3);
                switch(y)
                {
                    case 1:
                        Console.WriteLine("石头");
                        break;
                    case 2:
                        Console.WriteLine("剪刀");
                        break;
                    case 3:
                        Console.WriteLine("布");
                        break;
                }
                z = y - b;
                switch(z)
                {
                    case 0:
                        Console.WriteLine("平手");
                        break;
                        case -2:
                        Console.WriteLine("赢了");
                        break;
                        case-1:
                        Console.WriteLine("输了");
                        break;
                        case 1:
                        Console.WriteLine("赢了");
                        break;
                        case 2:
                        Console.WriteLine("输了");
                        break;
                }


    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 jie = 1;
                    for (int j = 1; j <= i; j++)
                    {
                        jie *= j;
                    }
                    sum += jie;
                }
                Console.Write(sum);
                Console.ReadLine();


    10.找出100以内与7有关的数并打印,并求出他们的和。利用for循环+if。
                7是个位数的    a%10==7
                7是十位数        a/10==7
                7的倍数          a%7==0
                int sum = 0;
                for (int a = 1; a <= 100; a++)
                {
                    if (a % 10 == 7 || a / 10 == 7 || a % 7 == 0)
                    {
                        Console.WriteLine(a);
                        sum += a;
                    }
                }
                Console.WriteLine(sum);
                Console.ReadLine();

  • 相关阅读:
    day50_BOS项目_02
    day51_BOS项目_03
    Criteria 和 DetachedCriteria 的区别
    ecplise中如何关闭Tomcat的自动重启/加载
    Tomcat的Start可以启动起来,但是Debug启动突然启动不起来,一直停在 Class<T>.getDeclaredConstructors0(boolean) line: not available [native method] 的解决办法
    windows下读取Linux分区软件
    15 条实用 Linux/Unix 磁带管理命令
    Nginx安装与配置文件解析
    专注docker安全:Security Scanning
    清除linux系统的多余引导
  • 原文地址:https://www.cnblogs.com/panyiquan/p/5199196.html
Copyright © 2011-2022 走看看