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