判断邮箱 判断验证码

练习:判断邮箱格式是否正确
1.有且只能有一个@
2.不能以@开头
3.@之后至少有一个.
4.@和.不能靠在一起
5.不能以.结尾

 1  Console.Write("请输入邮箱地址:");
 2             string m = Console.ReadLine();
 3             if (m.IndexOf("@") == m.LastIndexOf("@"))
 4             {
 5                 if (m.IndexOf("@") != 0)
 6                 {
 7                     string a = m.Substring(m.IndexOf("@"));
 8                     bool b=a.Contains(".");
 9                     if (b == true)
10                     {
11                         if (a.IndexOf(".") != 1)
12                         {
13                             if (m.Length - 1 != m.LastIndexOf("."))
14                             {
15                                 Console.WriteLine("您输入的格式正确!");
16                             }
17                             else
18                             {
19                                 Console.WriteLine("您输入的格式有误!");
20                             }
21                         }
22                         else
23                         {
24                             Console.WriteLine("您输入的格式有误!");
25                         }
26                     }
27                     else
28                     {
29                         Console.WriteLine("您输入的格式有误!");
30                     }
31                 }
32                 else
33                 {
34 
35                     Console.WriteLine("您输入的格式有误!");
36                 }
37             }
38             else
39             {
40                 Console.WriteLine("您输入的格式有误!");
41             }
42 
43             Console.ReadLine();

 练习:随机出一个四位数的验证码,然后输入验证码后判断与出的验证码是否一致

 1  for (; ; )
 2             {
 3                 string s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";//从里面随机抽
 4                 Random ran = new Random();
 5                 string x = "";
 6                 for (int i = 1; i <= 4; i++)
 7                 {
 8                     x += s.Substring(ran.Next(s.Length), 1);      //随机抽4位验证码  
 9                 }
10                 Console.WriteLine(x);
11                 Console.WriteLine("请输入验证码:");
12                 string m = Console.ReadLine();                    //自己输入验证码
13                 if (m.ToLower() == x.ToLower())                  //判断是否一致
14                 {
15                     Console.WriteLine("您的输入正确!");
16                     break;
17                 }
18                 else
19                 {
20                     Console.WriteLine("您的输入有误!");
21                     Console.ReadLine();
22                     Console.Clear();
23 
24                 }
25             }
26 
27             Console.ReadLine();
28            

 

posted @ 2016-03-11 21:59  冷风~云  阅读(308)  评论(0编辑  收藏  举报