【2017-12-08】c#基础-程序开发思路初认识

以验证邮箱为例:

“请输入您的邮箱:”
1-“邮箱正确!/错误!”
2-“只能有一个@符号”
3-“不能以@开头”
4-“不能以@结尾”
5-“@之后必须有点”
6-“@之后不能是点”
7-最少一个点,最多两个点
8-“不能以点结尾”
9-不能以数字结束

  1        #region 定义变量           
  2             //1 -“邮箱正确!/ 错误!”,end放置最终结果
  3             string end = "邮箱正确!";
  4             //2 -“只能有一个 @符号”
  5             bool atOnlyOne = true;
  6             //3 -“不能以 @开头”
  7             bool atStart = true;
  8             //4 -“不能以 @结尾”
  9             bool atEnd = true;
 10             //5 -“@之后必须有点”
 11             bool atDian = true;
 12             //6 -“@之后不能是点”
 13             bool atNoDian = true;
 14             //7 - 最少一个点,最多两个点
 15             bool dianOneOrTwo = true;
 16             //8 -“不能以点结尾”
 17             bool dianEnd = true;
 18             //9 - 不能以数字结束
 19             bool numEnd = true;
 20         #endregion      
 21 
 22             //让用户输入邮箱
 23             Console.Write("请输入你的邮箱地址:");
 24             string user_mail = Console.ReadLine();
 25                 
 26             if (user_mail.Length > 0)
 27             {
 28                 #region 只能有一个 @符号
 29                 int a1 = user_mail.IndexOf("@");
 30                 if (a1 == -1)
 31                 {
 32                     atOnlyOne = false;
 33                     end = "邮箱格式错误!";
 34                 }
 35                 else
 36                 {
 37                     int a2 = user_mail.IndexOf("@",a1+1);
 38                     if (a2 != -1)
 39                     {
 40                         atOnlyOne = false;
 41                         end = "邮箱格式错误!";
 42                     }
 43                 }
 44                 #endregion
 45 
 46                 #region 不能以 @开头
 47                 if (user_mail.StartsWith("@"))
 48                 {
 49                     atStart = false;
 50                     end = "邮箱格式错误!";
 51                 }
 52                 #endregion
 53 
 54                 #region 不能以 @结尾
 55                 if (user_mail.EndsWith("@"))
 56                 {
 57                     atEnd = false;
 58                     end = "邮箱格式错误!";
 59                 }
 60                 #endregion
 61 
 62                 #region @之后必须有点
 63                 if (atOnlyOne == true)
 64                 {
 65                     string a3 = user_mail.Substring(user_mail.IndexOf("@"));
 66                     if (!a3.Contains("."))
 67                     {
 68                         atDian = false;
 69                         end = "邮箱格式错误!";
 70                     }
 71                 }
 72                 #endregion
 73 
 74                 #region@之后不能是点
 75                 if (user_mail.IndexOf("@.") != -1)
 76                 {
 77                     atNoDian = false;
 78                     end = "邮箱格式错误!";
 79                 }
 80                 #endregion
 81 
 82                 #region 最少一个点,最多两个点
 83                 int count = 0;
 84                 int a5 = -1;
 85                 while (true)
 86                 {
 87                     a5 = user_mail.IndexOf(".", a5 + 1);
 88                     if (a5 != -1)
 89                         count++;
 90                     else
 91                         break;
 92                 }
 93                 if (count != 1 && count != 2)
 94                 {
 95                     dianOneOrTwo = false;
 96                     end = "邮箱错误!";
 97                 }
 98                 #endregion
 99 
100                 #region 不能以点结尾
101                 if (user_mail.EndsWith("."))
102                 {
103                     dianEnd = false;
104                     end = "邮箱格式错误!";
105                 }
106                 #endregion
107 
108                 #region 不能以数字结束
109                 string a6 = user_mail.Substring(user_mail.Length - 1, 1);
110                 try
111                 {
112                     Convert.ToInt32(a6);
113                     numEnd = false;
114                     end = "邮箱格式错误!";
115                 }
116                 catch { }
117                 #endregion
118 
119                 #region 打印结果
120                 if (atOnlyOne == false)
121                     Console.WriteLine("有且只有一个 @符号");
122                 else if (atStart == false)
123                     Console.WriteLine("不能以 @开头");
124                 else if (atEnd == false)
125                     Console.WriteLine("不能以 @结尾");
126                 else if (atDian == false)
127                     Console.WriteLine("@之后必须有点");
128                 else if (atNoDian == false)
129                     Console.WriteLine("@之后不能是点");
130                 else if (dianOneOrTwo == false)
131                     Console.WriteLine("最少一个点,最多两个点");
132                 else if (dianEnd == false)
133                     Console.WriteLine("不能以点结尾");
134                 else if (numEnd == false)
135                     Console.WriteLine("不能以数字结束");
136 
137                 Console.WriteLine(end);
138                 #endregion
139             }
140             else
141             {
142                 Console.WriteLine("邮箱不能为空!");
143             }

 把每一个小功能分成一小块去开发(#region...#endregion可折叠),哪里出错修改哪里,便于修改,同时便于增加新功能。

posted @ 2017-12-08 15:55  Int64  阅读(103)  评论(0编辑  收藏  举报