求教C#高手,设计程序

来源:百度知道 编辑:UC知道 时间:2024/05/16 12:16:15
设计一个Windows应用程序,要求单击[开始]按钮,将两个组合框中指定的年份范围内的所有闰年输出到列表框中.单击[清除]按钮,清除列表框中的内容.
代码是什么?求教C#高手!
我的开始代码是:
if (cb1.Text == "" || cb2.Text == "")
{ return; }
int
yearstart = int.Parse(cb1.Text);
yearend= int.Parse(cb2.Text);
if (yearstart > yearend)
return;
lo1.Items.Clear();
for (int a = yearstart; a <= yearend; a++)
if (a % 4 == 0 && a % 100 != 0 || a % 400 == 0)
lo1.Items.Add(a)
其中[开始]按钮是bt1,[清除]按钮是bt2,[组合框1]是cb1,[组合框2]是cb2,[列表框]是lo1.代码是哪里不对啊?是不是要定义个什么??

估计你是不知道闰年的算法,其他的你应该都没有问题了吧。
闰年的算法很简单。能被4整除的年份,都是闰年。
那么你要找出两个组合框指定的年份间的闰年,很简单啊。
for(int i=(int)combobox1.text;i<=(int)combobox2.text;i++)
{
if(i%4=0)
{
listbox.itmes.add(i.tostring());
}
}
这样不就可以了???
其他的代码,你自己完善吧。

楼上的闰年测试不准确。

If n Mod 400 = 0 Or (n Mod 4 = 0 And n Mod 100 <> 0)

Then

MsgBox s + "是闰年!"