在C#中实现一个基于字符串的日历

本文关键字:一个 字符串 日历 实现 | 更新日期: 2023-09-27 18:27:36

I希望创建一个C#程序,该程序显示日历并使用包含日期名称的字符串:

string Names = "Sun,Mon,Tues,Wed,Thurs,Fri,Sat,";

到目前为止我已经。。。。

int weeks = 1;
int days = 1;
string Names = "Sun,Mon,Tues,Wed,Thurs,Fri,Sat,";
string dayName;
int commaIndex = 0;
int date = 1;
while (weeks < 5) 
{ 
    while (days < 8) 
    { 
        commaIndex = Names.IndexOf(","); // find the period 
        dayName = Names.Remove(commaIndex); 
        lblCalendar.Text += dayName + "." + " " + date + " "; 
        Names = Names.Remove(0, commaIndex + 1);
        days++; 
        date++; 
    } 
    weeks++;
} 

但这只写在第一周。。有人能帮我找出哪里出了错误吗?

在C#中实现一个基于字符串的日历

首先,您忘记在每次迭代后重新启动days变量。此外,在第一次迭代之后,Names字符串为空。我建议创建一个日期名称数组,并使用它来代替一个字符串。

        int weeks = 1;
        int days = 1;
        var Names = new[] {"Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
        string dayName;
        int commaIndex = 0;
        int date = 1;
        while (weeks < 5)
        {
            while (days < 8)
            {
                dayName = Names[days-1];
                lblCalendar.Text += dayName + "." + " " + date + " ";
                days++;
                date++;
            }
            days = 1;
            weeks++;
        }

您忘记重置天数。

while (weeks < 5) 
{ 
    days = 1
    string Names = "Sun,Mon,Tues,Wed,Thurs,Fri,Sat,";
    while (days < 8) 
    {