生活随笔
收集整理的這篇文章主要介紹了
【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可折疊),哪里出錯修改哪里,便于修改,同時便于增加新功能。
轉載于:https://www.cnblogs.com/zqyyx/p/8005778.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的【2017-12-08】c#基础-程序开发思路初认识的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。