通过键盘输入某年某月某日,计算并输出这一天是这一年的第几天。例如,2001年3月5日是这一年的第64天。注意:使用分支结构语句实现。

using System;

namespace 日期
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入年");
            int Y = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入月");
            int M = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入日");
            int D = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(Y + "年" + M + "月" + D + "日是今年的第" + Date(Y,M, D) + "天");
        }

        static int Date(int y, int m, int d)

        {
            //定义b装天数
            int b = 0;
            //数组装天数
            int[] array = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
            //除2月外还有11个月,所以i=1,i<12
            for (int i = 1; i < m; i++)
            {
                //闰年
                if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
                {
                    b = b + array[i];
                }
                //平年
                else
                {
                    array[1] = 28;
                    b = b + array[i];
                }

            }
            d = d + b;
            //返回天数
            return d;
        }
    }
}


版权声明:本文为muziiii原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。