提问:c#实习内容,利用窗体计算闰年

c#利用窗体计算某一阶段内所有闰年

img

用Int.Parse将输入框转数字,然后for遍历,写个函数判断是否闰年,是加入到listview中

题主要的代码如下,有帮助麻烦点个采纳【本回答右上角】,谢谢~~有其他问题可以继续交流~

img

如需工程文件请站内短信发邮箱地址。~

using System;
using System.Windows.Forms;
using Fleck;
using System.Security.Cryptography.X509Certificates;
using System.Collections.Generic;

namespace WindowsFormsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        bool isRN(int year)
        {
            return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox2.Text = "";
            listView1.Items.Clear();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool err = false;
            int startYear, endYear;
            if (!int.TryParse(textBox1.Text, out startYear))
            {
                textBox1.Select();
                errorProvider1.SetError(textBox1, "起始年度为数字!");
                err = true;
            }
            else if (startYear < 0)
            {
                textBox1.Select();
                errorProvider1.SetError(textBox1, "起始年度需要大于0!");
                err = true;
            }
            if (!int.TryParse(textBox2.Text, out endYear))
            {
                textBox2.Select();
                errorProvider1.SetError(textBox2, "终止年度为数字!");
                err = true;
            }
            else if (endYear < 0)
            {
                textBox2.Select();
                errorProvider1.SetError(textBox2, "终止年度需要大于0!");
                err = true;
            }
            if (startYear > endYear) {
                textBox2.Select();
                errorProvider1.SetError(textBox2, "终止年度需要大于起始年度!");
                err = true;
            }
            if(err)return;
            errorProvider1.Clear();
            for (var i = startYear; i <= endYear; i++)
            {
                if (isRN(i)) listView1.Items.Add(i.ToString());
            }
        }
    }
}




参考一下:

img


string Yi = this.textBox1.Text;//获取输入第一个值
string er = this.textBox3.Text;//获取输入第二个值
int l = 0;//循环加值使用
string ee = "";//闰年拼接
if (Convert.ToInt32(er) < Convert.ToInt32(Yi))
{
this.errorProvider1.SetError(this.textBox2, "第二个值不可小于第一个值");
return;
}
for (int i = 0; i < Convert.ToInt32(er) - Convert.ToInt32(Yi); i++)
{
if (((Convert.ToInt32(Yi) + l) % 400 == 0) || ((Convert.ToInt32(Yi) + l) % 4 == 0 && (Convert.ToInt32(Yi) + l) % 100 != 0))
{
ee = ee + " " + (Convert.ToInt32(Yi) + l);
}
l = l + 1;
}
if (string.IsNullOrEmpty(ee))
{
this.errorProvider1.SetError(this.textBox2, "无闰年");
return;
}
this.textBox2.Text = ee;