c#简单日历怎么实现

2024-12-30 14:26:11
推荐回答(1个)
回答1:

你想做什么  ??简单到什么程度,你看这样可以不

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                int year, month;//分别声明接收用户输入年月的变量
                while (true)
                {
                    Console.Write("请输入年份(1900-2100):");
                    year = int.Parse(Console.ReadLine());
                    if (year < 1900 || year > 2100)
                    {
                        Console.Write("输入的年份不在1900-2100之间,请按会车键重新输入!");
                        Console.ReadLine();
                        Console.Clear();
                    }
                    else
                    {
                        Console.Write("请输入月份(1-12):");
                        month = int.Parse(Console.ReadLine());
                        if (month < 1 || month > 12)
                        {
                            Console.Write("输入的月份不在1-12之间,请按回车键重新输入!");
                            Console.ReadLine();
                            Console.Clear();
                        }
                        else
                            break;
                    }
                }

                List dataes = new List();
                //分别声明用户输入的年月与已知1900年1月1日相隔的整年天数和月份天数
                int crossDayToYear = 0, crossDayToMonth = 0;
                //1900年到year-1年相隔的天数
                for (int i = 1900; i < year; i++)
                {
                    if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
                        crossDayToYear += 366;
                    else
                        crossDayToYear += 365;
                }

                for (int i = 1; i < month; i++)
                {
                    if (i == 2)
                    {
                        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                            crossDayToMonth += 29;
                        else
                            crossDayToMonth += 28;
                    }
                    else if (i <= 7 && i % 2 != 0 || i > 7 && i % 2 == 0)
                        crossDayToMonth += 31;
                    else
                        crossDayToMonth += 30;
                }
                int crossDay = crossDayToYear + crossDayToMonth;//相隔的总天数
                int dayOfWeek = crossDay % 7 + 1;//用户输入的月份第一天是星期几
                int space = dayOfWeek - 1;
                for (int i = 0; i < space; i++)
                {
                    dataes.Add("");
                }

                int days;//用户输入的月份的天数
                if (month == 2)
                {
                    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                        days = 29;
                    else
                        days = 28;
                }
                else if (month <= 7 && month % 2 != 0 || month > 7 && month % 2 == 0)
                    days = 31;
                else
                    days = 30;
                for (int i = 1; i <= days; i++)
                {
                    dataes.Add(i.ToString());
                }
                Console.WriteLine("******************************************************");
                Console.Write("一\t二\t三\t四\t五\t六\t日");
                for (int i = 0; i < dataes.Count; i++)
                {
                    if (i % 7 == 0)
                        Console.WriteLine();
                    Console.Write(dataes[i] + "\t");
                }
                Console.WriteLine();
                Console.WriteLine("******************************************************");
                Console.Write("按回车键继续");
                Console.ReadLine();
                Console.Clear();
            }
            
        }
   
    }
    

}