C# - 编写一个显示日历的程序,只询问年和月,使用 System.DateTime

C# - write a program that displays the calendar, asking just for the year and month, use System.DateTime

我坚持这个,我需要帮助我如何要求用户输入输入任何年份和任何月份,然后将输出指定年份和月份的月历,我还需要使用 system.datetime 这是我到目前为止的代码,我认为它不正确,我们将不胜感激。谢谢

class Program
{
    static int year = new int();
    static int month = new int();
    static int[,] calendar = new int[6, 7];

    static void Main(string[] args)
    {
        Console.Write("Enter the year? ");
        year = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter the month (January = 1, etc): ");
        month = Convert.ToInt32(Console.ReadLine());

        DrawHeader();
        FillCalendar();
        DrawCalendar();
        Console.ReadLine();
    }

    static void DrawHeader()
    {
        Console.Write("\n\n");
        Console.WriteLine("\t" + month);
        Console.WriteLine("Mo Tu We Th Fr Sa Su");
    }

    static void FillCalendar()
    {
        int days = DateTime.DaysInMonth(year, month);
        int currentDay = 1;
        for (int i = 0; i < calendar.GetLength(0); i++)
        {
            for (int j = 0; j < calendar.GetLength(1) && currentDay <= days; j++)
            {
                if (i == 0 && month > j)
                {
                    calendar[i, j] = 0;
                }
                else
                {
                    calendar[i, j] = currentDay;
                    currentDay++;
                }
            }
        }
    }

    static void DrawCalendar()
    {
        for (int i = 0; i < calendar.GetLength(0); i++)
        {
            for (int j = 0; j < calendar.GetLength(1); j++)
            {
                if (calendar[i, j] > 0)
                {
                    if (calendar[i, j] < 10)
                    {
                        Console.Write(" " + calendar[i, j] + " ");
                    }
                    else
                    {
                        Console.Write(calendar[i, j] + " ");
                    }
                }
                else
                {
                    Console.Write("   ");
                }
            }
            Console.WriteLine("");
        }
    }
}

这应该确保每月的第几天与星期几正确对齐(即 12 月 1 日是星期二)

using System;
using System.Globalization;

namespace Calendar
{
class Program
{
    static int year = new int();
    static int month = new int();
    static int[,] calendar = new int[6, 7];
    private static DateTime date;

    static void Main(string[] args)
    {
        Console.Write("Enter the year? ");
        year = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter the month (January = 1, etc): ");
        month = Convert.ToInt32(Console.ReadLine());

        date = new DateTime(year, month, 1);//gives you a datetime object for the first day of the month
        DrawHeader();
        FillCalendar();
        DrawCalendar();
        Console.ReadLine();
    }

    static void DrawHeader()
    {
        Console.Write("\n\n");
        //gives you the month and year at the top of the calendar
        Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month) + " " + year);
        Console.WriteLine("Mo Tu We Th Fr Sa Su");
    }

    static void FillCalendar()
    {
        int days = DateTime.DaysInMonth(year, month);
        int currentDay = 1;
        var dayOfWeek = (int) date.DayOfWeek;
        for (int i = 0; i < calendar.GetLength(0); i++)
        {
            for (int j = 0; j < calendar.GetLength(1) && currentDay - dayOfWeek + 1 <= days; j++)
            {
                if (i == 0 && month > j)
                {
                    calendar[i, j] = 0;
                }
                else
                {
                    calendar[i, j] = currentDay - dayOfWeek + 1;
                    currentDay++;
                }
            }
        }
    }

    static void DrawCalendar()
    {
        for (int i = 0; i < calendar.GetLength(0); i++)
        {
            for (int j = 0; j < calendar.GetLength(1); j++)
            {
                if (calendar[i, j] > 0)
                {
                    if (calendar[i, j] < 10)
                    {
                        Console.Write(" " + calendar[i, j] + " ");
                    }
                    else
                    {
                        Console.Write(calendar[i, j] + " ");
                    }
                }
                else
                {
                    Console.Write("   ");
                }
            }
            Console.WriteLine("");
        }
    }
}

}

编辑

如果您想获取所选月份中一周特定日期的所有日期,您可以这样做;

        var weekDay = 0;
        var dates = new List<DateTime>();
        for (int i = 0; i < days; i++)
        {
            if ((int)date.AddDays(i).DayOfWeek == weekDay)
            {
                dates.Add(date.AddDays(i));
            }
        }

您必须要求用户输入星期几并将 weekDay 设置为他们输入的值。请记住,星期日是 0,星期六 = 6。(不过我还没有完全测试过,所以要小心)- 这必须输入到您的 FillCalendar 方法中。