带有 stringbuilder 日历的控制台日历 sheet

Console Calendar with stringbuilder calendar sheet

我正在将基于控制台的日历作为一项教育任务(学校)。它需要显示用户输入的特定月份的日历 sheet。我快到了,但我注意到现在所有月份都从星期一开始。我用 stringbuilder 创建了 sheet,我试图用零填充该月第一天之前的日子,但它还没有用。 for循环是我的尝试。有人有什么想法吗?我还在学习。这是构建 sheet 的代码,除了那个问题之外它工作正常:

DateTime date = DateTime.Now;
        List<DateTime> dateList = new List<DateTime>();

        DateTime dateCopyForModification = date;

        int inputMonth = date.Month;

        while (dateCopyForModification.Month == inputMonth)
        {
            dateList.Add(dateCopyForModification);
            dateCopyForModification = dateCopyForModification.AddDays(1);
        }

        Console.WriteLine("\t---------\n\t{0}, {1}\n\t---------", date.ToString("MMMM"), date.Year);

        int dayCounter = 0;
        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.Append("\nMo\t|\tDi\t|\tMi\t|\tDo\t|\tFr\t|\tSa\t|\tSo\t|\n-----------------------------------------------------------------------------------------------------\n");


        foreach (DateTime dateTime in dateList)
        {

//the part that doesnt work
            int day = (int)dateTime.DayOfWeek;

            if (dateTime == new DateTime(dateTime.Year, dateTime.Month, 1))
            {
                for (day = 1; day == (int)dateTime.DayOfWeek; day++)
                {
                    stringBuilder.Append("0");
                    dayCounter++;
                }
            }
//until here
            else
            {
                stringBuilder.Append(dateTime.Day);
                dayCounter++;
            }

            if (dateTime.Day == 28)
            {
                stringBuilder.Append("  |");
            }
            else
            {
                stringBuilder.Append("\t|\t");
            }

            if (dayCounter == 7)
            {
                stringBuilder.Append("\n");
                dayCounter = 0;
            }

        }
        Console.Write(stringBuilder);
        Console.ReadKey();

我为您创建了一个小解决方案 problem.However 它不使用 stringbuilder

//This class will store the given month information
class MonthInfo
{
    public DayOfWeek DayName { get; set; }
    public DateTime DayDate { get; set; }
}

//This class will store the cursor position, just to make it look like a calendar
class CursorPosition
{
    public string DayName { get; set; }
    public DayOfWeek DayWeek { get; set; }
    public int locationx { get; set; }


}

这是代码的其余部分

 static void Main(string[] args)
    {
        int monthdigit = 0;
        DateTime _date;


        Console.WriteLine("Enter Month");
        string monthName= Console.ReadLine(); //should be in format "Jan","Feb"


        if (DateTime.TryParseExact(monthName, "MMM", CultureInfo.InvariantCulture, DateTimeStyles.None, out _date))
        {
            monthdigit = (int)_date.Month;
        }
        else
        {
            Console.WriteLine("invalid..programm will exit");
            return;
        }




        List<MonthInfo> GetMyDate = GetDates(2016, monthdigit);


         //stores the cursor position to align the days accordingly
        List<CursorPosition> CurorList = new List<CursorPosition>();
        CurorList.Add(new CursorPosition() { DayName = "Sun", DayWeek=DayOfWeek.Sunday });
        CurorList.Add(new CursorPosition() { DayName = "Mon", DayWeek = DayOfWeek.Monday });
        CurorList.Add(new CursorPosition() { DayName = "Tue", DayWeek = DayOfWeek.Tuesday });
        CurorList.Add(new CursorPosition() { DayName = "Wed", DayWeek = DayOfWeek.Wednesday });
        CurorList.Add(new CursorPosition() { DayName = "Thu", DayWeek = DayOfWeek.Thursday });
        CurorList.Add(new CursorPosition() { DayName = "Fri", DayWeek = DayOfWeek.Friday });
        CurorList.Add(new CursorPosition() { DayName = "Sat", DayWeek = DayOfWeek.Saturday });


        //print all the days name
        foreach (CursorPosition _activeCursor in CurorList)
        {
              Console.Write("\t{0}",_activeCursor.DayName);
              _activeCursor.locationx = Console.CursorLeft;

        }



       //retreive the cursor position and display your day index by adjusting the rownumber accordingly.
        int _dayIndex = 1;
        int rownumber = 2;

        foreach (MonthInfo _month in GetMyDate)
        {
            Console.WriteLine();

                int positionx = (from p in CurorList
                                 where p.DayWeek == _month.DayName
                                 select p.locationx).Single();



            Console.SetCursorPosition(positionx, rownumber + 1);
            Console.Write(_dayIndex++.ToString());



                if ((int)_month.DayName== 6)
                {
                    rownumber++;
                    Console.WriteLine();
                }



        }

        Console.ReadKey();

    }

  public static List<MonthInfo> GetDates(int year, int month)
    {
        var query=from date in  Enumerable.Range(1, DateTime.DaysInMonth(year, month))  // Days: 1, 2 ... 31 etc.
                  select new MonthInfo() { DayName = new DateTime(year, month, date).DayOfWeek, DayDate = new DateTime(year, month, date) };

        return query.ToList();
    }