迟到的作业。

要求:检测输入是否为闰年

具体实现方法:1、限制字符串长度(其实可以不限制)2、限制输入内容(正则表达式只允许0-9,不考虑年份为负数的情况)3、int.parse()函数并未考虑限制类似0004这样的输入,所以同样不做限制)

测试用例如下

主要代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions; 

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Regex regex = new Regex(@"^[0-9]+$");
            String text = textBox1.Text;
            String result = null;
            if (text.Length > 6 || text.Length < 1)
            {
                result="length wrong!";
            }
            else if (!regex.IsMatch(text))
            {
                result="ilegal input!";
            }
            else
            {
                int year  = int.Parse(text);
                if (year % 400 == 0)
                {
                    result = "YES";
                }
                else if (year % 100 == 0)
                {
                    result = "NO";
                }
                else if (year % 4 == 0)
                {
                    result = "YES";
                }
                else
                {
                    result = "NO";
                }
            }

            label2.Text = result;
        }
    }
}
View Code

全部文件如下(VS2013)

https://files.cnblogs.com/files/limiting/%E9%97%B0%E5%B9%B4%E6%A3%80%E6%B5%8B.rar

原文地址:https://www.cnblogs.com/limiting/p/4402260.html