寒假作业(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();

你可能感兴趣的:(寒假作业(2))