首页 技术 正文
技术 2022年11月20日
0 收藏 524 点赞 4,045 浏览 1203 个字

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

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

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

看代码

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace _11._1练习
{
class Program
{
static void Main(string[] args)
{
while (true)
{ //随机验证码 整型
Random x = new Random();
int ranx = x.Next(, ); //把整型的随机数转换成字符串,方便进行字符串比对
string ran = Convert.ToString(ranx); //打印验证码
Console.WriteLine("验证码:" + ranx);
Console.WriteLine();
Console.Write("验 证:"); //获取用户输入的内容
string user = Console.ReadLine(); //防止用户误操作,把空格替换成空字符串
string str = user.Replace(" ", ""); //检测用户输入内容的长度,长度符合---下一步,长度不符合---长度错误
int strleg = str.Length;
if (strleg == )
{
//开始比对字符串 if (str == ran)
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("验证成功!!!");
break;
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine();
Console.WriteLine("输入错误!!!");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine();
}
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine();
Console.WriteLine("长度错误!!!");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine();
}
} Console.ReadLine();
}
}
}

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

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

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

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,027
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,365
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,780
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,857