闰年测试和非法输入

闰年测试和非法输入

题目

编写一个程序,测试输入年份是否为闰年,考虑输入不合法的情况。


简单实现

实现判断闰年的功能很简单,只需要将输入字符串转换为数字,然后模运算判断。

 1     public partial class Form1 : Form
 2     {
 3         public Form1()
 4         {
 5             InitializeComponent();
 6         }
 7 
 8         private void button1_Click(object sender, EventArgs e)
 9         {
10             string messageStr = "";
11             switch (CheckIfLeapYear())
12             {
13                 case 0:
14                     messageStr = textBox1.Text + " is not leap year.";
15                     break;
16                 case 1:
17                     messageStr = textBox1.Text + " is leap year.";
18                     break;
19             }
20             MessageBox.Show(messageStr);
21         }
22 
23         int CheckIfLeapYear()
24         {
25             int year;
26             year = int.Parse(textBox1.Text);
27 
28             if (year == 0)
29                 return 2;
30             if (year % 400 == 0)
31                 return 1;
32             if (year % 4 == 0 && year % 100 != 0)
33                 return 1;
34             return 0;
35         }
36     }


考虑输入含有非数字字符的情况

如果输入含有非数字字符的话,以上的代码就会抛出异常。

解决方法很简单,在C#中可以使用try...catch...,或者TryParse。

 1     public partial class Form1 : Form
 2     {
 3         public Form1()
 4         {
 5             InitializeComponent();
 6         }
 7 
 8         private void button1_Click(object sender, EventArgs e)
 9         {
10             string messageStr = "";
11             switch (CheckIfLeapYear())
12             {
13                 case 0:
14                     messageStr = textBox1.Text + " is not leap year.";
15                     break;
16                 case 1:
17                     messageStr = textBox1.Text + " is leap year.";
18                     break;
19                 case 2:
20                     messageStr = textBox1.Text + " is not a number.";
21                     break;
22             }
23             MessageBox.Show(messageStr);
24         }
25 
26         int CheckIfLeapYear()
27         {
28             int year;
29             try
30             {
31                 year = int.Parse(textBox1.Text);
32             }
33             catch (Exception e)
34             {
35                 return 2;
36             }
37             if (year == 0)
38                 return 2;
39             if (year % 400 == 0)
40                 return 1;
41             if (year % 4 == 0 && year % 100 != 0)
42                 return 1;
43             return 0;
44         }
45     }


更特殊的情况

Parse、Convert这些函数能够简单高效地实现类型转换的功能,但是还是有问题存在。

就题目来说,输入的是一个年份,但是输入不一定是int能够装下的(我一直相信人类能够生存到超过32位有符号整数的年份)。如果输入一个超过int,甚至int64的年份,将字符串转换为某个数据类型就不合理了。

所以,应该手动判断字符串是否合法、输入年份是否为闰年。

 1     public partial class Form1 : Form
 2     {
 3         public Form1()
 4         {
 5             InitializeComponent();
 6         }
 7 
 8         private void button1_Click(object sender, EventArgs e)
 9         {
10             string messageStr = "";
11             switch (CheckIfLeapYear())
12             {
13                 case 0:
14                     messageStr = textBox1.Text + " is not leap year.";
15                     break;
16                 case 1:
17                     messageStr = textBox1.Text + " is leap year.";
18                     break;
19                 case 2:
20                     messageStr = textBox1.Text + " is not a number.";
21                     break;
22             }
23             MessageBox.Show(messageStr);
24         }
25         
26         int CheckIfLeapYear()
27         {
28             int last3Number = 0;//如果输入合法的话只有后三位是有用的
29             string str = textBox1.Text;
30             int s = 0;
31             bool allZero = true;//没有0这个年份,如果全是0的话不是闰年
32             if (str[0] == '-')
33                 s = 1;
34             for (int i = s; i < str.Length; i++)
35             {
36                 if (str[i] < '0' || str[i] > '9')
37                     return 2;
38                 if (str[i] != '0')
39                     allZero = false;
40                 if (str.Length - i <= 3)
41                     last3Number = last3Number * 10 + (int)(str[i] - '0');
42             }
43             if (allZero)
44                 return 2;
45             if (last3Number % 400 == 0)
46                 return 1;
47             if (last3Number % 4 == 0 && last3Number % 100 != 0)
48                 return 1;
49 
50             return 0;
51         }
52     }

 

原文地址:https://www.cnblogs.com/fks143/p/4399229.html