c# - 在 C# 中实现基于字符串的日历

标签 c#

想要创建一个显示日历并使用包含日期名称的字符串的 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++;
} 

但这只写了第一周..任何人都可以帮我找出哪里有错误吗?

最佳答案

首先,您忘记在每次迭代后重新启动 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++;
        }

关于c# - 在 C# 中实现基于字符串的日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34405486/

相关文章:

c# - 如何从 web api 中的 Angular 帖子读取文件?

c# - 自定义包含TakeWhile(),有没有更好的办法?

c# - 任务 IsCanceled 是假的,而我取消了

c# - 配置.cs文件asp.net 5 mvc

c# - 我可以将 Visual Studio 设置为卸载所有项目,而不是选择 1 个项目并仅递归加载其依赖项吗?

c# - 从表中获取结果,将它们存储在一个数组中,然后显示它们

c# - MVC 4 "The using block is missing a } character"

c# - 在 C# 中构建文件路径的最佳实践

c# - 如何从 C# 应用程序中正确清理 Excel 互操作对象?

ViewModel 中的 c# 空引用