我希望创建一个显示日历并使用包含日期名称的字符串的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++;
}
但那只写了第一周..有人能帮我弄清楚哪里有错误吗?
答案 0 :(得分:3)
首先,您忘记在每次迭代后重新启动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++;
}
答案 1 :(得分:2)
你忘了重置日子。
while (weeks < 5)
{
days = 1
string Names = "Sun,Mon,Tues,Wed,Thurs,Fri,Sat,";
while (days < 8)
{