帶有 stringbuilder 日曆表的控制台日曆 (Console Calendar with stringbuilder calendar sheet)


問題描述

帶有 stringbuilder 日曆表的控制台日曆 (Console Calendar with stringbuilder calendar sheet)

我正在開發基於控制台的日曆作為教育任務(學校)。它需要顯示用戶輸入的特定月份的日曆表。我快到了,但我注意到現在所有月份都從星期一開始。我使用字符串生成器創建了工作表,並嘗試在該月的第一天實際出現零之前的幾天填充,但它還不起作用。for循環是我的嘗試。有人有什麼想法嗎?我還在學習。這是構建工作表的代碼,除了該問題之外,它工作正常:

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();

參考解法

方法 1:

I created a small solution for your problem.However it doesn't use 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; }


}

and here is the rest of the code

 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();
    }

(by tweedledum11Rohit)

參考文件

  1. Console Calendar with stringbuilder calendar sheet (CC BY‑SA 2.5/3.0/4.0)

#Console #stringbuilder #calendar #C#






相關問題

grails 日誌消息未顯示在 STS 3.0 控制台上 (grails log messages not displaying on STS 3.0 console)

在 AppFog 上,如何在終端(node.js)上查看控制台日誌 (on AppFog, how to see the console logs on the terminal (node.js))

控制台應用程序中的多個參數未正確解析 (Multiple args in Console Application not parsing correctly)

WPF 應用程序沒有輸出到控制台? (No output to console from a WPF application?)

用 .NET 4.5 編寫的 Windows 服務中的 Console.Out 和 Console.Error 競爭條件錯誤 (Console.Out and Console.Error race condition error in a Windows service written in .NET 4.5)

Visual Basic 2013 - 控制台輸入令牌? (Visual Basic 2013 - Console Input Tokens?)

帶有 stringbuilder 日曆表的控制台日曆 (Console Calendar with stringbuilder calendar sheet)

增加和減少混淆 (Incrementing and Decrementing Confusion)

有沒有辦法在標準 C++ 中直接從鍵盤讀取輸入? (Is there a way to read input directly from the keyboard in standard C++?)

是否可以在方案中編寫控制台應用程序? (Is it possible to write console applications in scheme?)

犀牛的控制台輸入功能? (console input function for rhino?)

輸出中的 PowerShell 空白 (PowerShell gaps in output)







留言討論