Software test--c# The form applet to Judge Leap year

Source: Internet
Author: User

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;

Namespace Softwartest_leapyear
{
public partial class Form1:form
{
string yearstr = null;
int year = 0;
BOOL Isleapyear = false;
BOOL Isleapyear (int year)
{
If (year% 400 = = 0)
return true;
if (year% 4 = = 0 && Year% 100! = 0)
return true;
Else
return false;
}
Public Form1 ()
{
InitializeComponent ();
}

private void But_judge_click (object sender, EventArgs e)
{
Text_output. Clear ();
Isleapyear = Isleapyear (year);
if (isleapyear)
Text_output. AppendText (yearstr.tostring () + "is a leap year:)");
else if (!isleapyear)
Text_output. AppendText (yearstr.tostring () + "is not a leap year: (");
Else
Text_output. AppendText ("Please input a correct year!");
}

private void Text_input_textchanged (object sender, EventArgs e)
{
Yearstr = This.text_input. Text;
year = Int. Parse (YEARSTR);
}

private void Text_output_textchanged (object sender, EventArgs e)
{

}
}
}

However, if you enter a string of non-pure numbers, Visual studio throws exception. Such as:

Now let's think about the solution.

We still call parse.

Then we should check the string yearstr before we pass the argument.

A regular expression method can be used here.

We modify the code as follows:

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 Softwartest_leapyear
{
public partial class Form1:form
{
string yearstr = null;
int year = 0;
BOOL Isleapyear = false;
BOOL Isyear = false;
public bool Isleapyear (int year)
{
If (year% 400 = = 0)
return true;
if (year% 4 = = 0 && Year% 100! = 0)
return true;
Else
return false;
}
public bool Isyear (string yearstr)
{
Regex r = new Regex (@ "^\d+$");
Return R.ismatch (YEARSTR);
}

Public Form1 ()
{
InitializeComponent ();
}

private void But_judge_click (object sender, EventArgs e)
{
Text_output. Clear ();
Isyear = Isyear (YEARSTR);
if (isyear)
{
year = Int. Parse (YEARSTR);
Isleapyear = Isleapyear (year);
if (isleapyear)
Text_output. AppendText (yearstr.tostring () + "is a leap year:)");
Else
Text_output. AppendText (yearstr.tostring () + "is not a leap year: (");
}
Else
Text_output. AppendText ("Please input a correct year!");
}

private void Text_input_textchanged (object sender, EventArgs e)
{
Yearstr = This.text_input. Text;
}

private void Text_output_textchanged (object sender, EventArgs e)
{

}
}
}

We use the function isyear to determine whether the input is a pure number (true for a pure number, otherwise return false) and return the result to the Boolean value isyear, and take advantage of the judgment statement, which is called only if the Isyear value is true.

Statement that converts a pure numeric string to an integer variable.

In a word, regular expressions are powerful tools that we should use in the process of programming. There is a chance to make a special list of the usage of regular expressions (o´ω ' O) dentetsu

Software test--c# The form applet to Judge Leap year

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.