C#简单的四位纯数字验证码

验证码练手,整型、四位验证码

大体意思就是:四位纯数字验证,只要验证不成功就无限验证

刚开始在纠结怎么让整个过程循环起来,什么循环放到最外层,其实就是一个循环,看来自己的循环练习的还是不够多,不够灵活

看代码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _11._1练习
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             while (true)
14             {
15 
16 
17                 //随机验证码     整型
18                 Random x = new Random();
19                 int ranx = x.Next(1000, 9999);
20 
21 
22 
23 
24                 //把整型的随机数转换成字符串,方便进行字符串比对
25                 string ran = Convert.ToString(ranx);
26 
27 
28 
29                 //打印验证码     
30                 Console.WriteLine("验证码:" + ranx);
31                 Console.WriteLine();
32                 Console.Write("验  证:");
33 
34 
35 
36 
37                 //获取用户输入的内容
38                 string user = Console.ReadLine();
39 
40 
41 
42                 //防止用户误操作,把空格替换成空字符串
43                 string str = user.Replace(" ", "");
44 
45 
46 
47 
48                 //检测用户输入内容的长度,长度符合---下一步,长度不符合---长度错误
49                 int strleg = str.Length;
50                 if (strleg == 4)
51                 {
52                     //开始比对字符串
53 
54                     if (str == ran)
55                     {
56                         Console.WriteLine();
57                         Console.ForegroundColor = ConsoleColor.Red;
58                         Console.WriteLine("验证成功!!!");
59                         break;
60                     }
61                     else
62                     {
63                         Console.ForegroundColor = ConsoleColor.Red;
64                         Console.WriteLine();
65                         Console.WriteLine("输入错误!!!");
66                         Console.ForegroundColor = ConsoleColor.White;
67                         Console.WriteLine();
68                     }
69                 }
70                 else
71                 {
72                     Console.ForegroundColor = ConsoleColor.Red;
73                     Console.WriteLine();
74                     Console.WriteLine("长度错误!!!");
75                     Console.ForegroundColor = ConsoleColor.White;
76                     Console.WriteLine();
77                 }
78             }
79 
80 
81 
82 
83             Console.ReadLine();
84         }
85     }
86 }

为了方便检查代码,加了挺多的注释,并且每个过程都会空格开,个人感觉这个习惯挺好,因为在检查代码或者是卡断点的时候,自己的思路更清晰、更透彻

嗯,放一张效果图吧,小黑窗其实也蛮有意思的

还是需要多练习,键盘上贴纸了,看什么时候能把键盘纸敲烂

转载于:https://www.cnblogs.com/TheJoker/p/7766588.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值