zoukankan      html  css  js  c++  java
  • HQ-day7 随机数案例:随机出验证码,对照输入,判断是否正确

     1 string s = "abcdefghiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
     2             for (; ; )
     3             {
     4                 Random ran = new Random();
     5                 int a = ran.Next(s.Length);   //随机抽取s内的字符a,b,c,d
     6                 int b = ran.Next(s.Length);
     7                 int c = ran.Next(s.Length);
     8                 int d = ran.Next(s.Length);
     9 
    10                 string a1 = s.Substring(a, 1);//接收a,b,c,d的字符串a1,b1,c1,d1
    11                 string b1 = s.Substring(b, 1);
    12                 string c1 = s.Substring(c, 1);
    13                 string d1 = s.Substring(d, 1);
    14                 string ss = a1 + b1 + c1 + d1;//组成验证码
    15                 Console.WriteLine(ss); //输出验证码
    16 
    17                 Console.Write("请输入验证码:");//用户输入验证码
    18                 string yzm = Console.ReadLine();
    19 
    20                 string ss1 = ss.ToUpper();  //将输入的验证码全部转化为大写字符(或者小写)
    21                 string yzm1 = yzm.ToUpper();
    22 
    23                 if (ss1 == yzm1)   //比对输入和显示是否一致
    24                 {
    25                     Console.WriteLine("输入正确");
    26                     break;
    27 
    28                 }
    29                 else
    30                 {
    31                     Console.WriteLine("您输入的有误!");
    32                 }
    33             }
    34             Console.ReadLine();

     简化代码:

     1   string s = "abcdefghiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
     2              for (; ; )
     3                 {
     4                     Random ran = new Random();
     5                
     6                     string biao = "";
     7                     for (int i = 1; i <= 4; i++)
     8                     {
     9                         biao += s.Substring(ran.Next(s.Length), 1);
    10                     }
    11                     Console.WriteLine(biao);
    12                     Console.Write("请输入验证码");
    13                     string shu = Console.ReadLine();
    14                     if (shu.ToUpper() == biao.ToUpper())
    15                     {
    16                         Console.WriteLine("输入正确");
    17                         break;
    18 
    19                     }
    20 
    21                     else
    22                     {
    23                         Console.Clear();
    24                         Console.WriteLine("输入错误");
    25                         
    26                     }
    27 
    28                 }
  • 相关阅读:
    时间戳(UnixTimestamp)与 《2038年问题》
    端口相关命令
    Ubuntu中的在文件中查找和替换命令
    A Reusable Aspect for Memory Profiling
    acc文件的运行
    A Reusable Aspect for Memory Allocation Checking
    ACC常用语句
    aspectC++常用命令
    c++调用DOS命令,不显示黑屏
    fopen文件目录问题
  • 原文地址:https://www.cnblogs.com/Itwonderful/p/5268003.html
Copyright © 2011-2022 走看看