using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 验证码字符串做法
{
class Program
{
static void Main(string[] args)
{
//定义一组字符串 验证码需要26个英文大小写和0~9的数字
string[] s= new string[62]
{"0","1","2","3","4","5","6","7","8","9",
"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s",
"t","u","v","w","x","y","z",
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q",
"R","S","T","U","V","W","X","Y","Z"
};
string a="";//定义一个a的字符变量,值为空
Random ran = new Random();//初始随机数
for (int i = 0; i < 4;i++ )//验证码需要四个字符串,需要循环四次,每次随机一个字符串连成验证码
{
int suijishu = ran.Next(62);//数组里有62个字符串,创建62以内的随机数,用来随机数组内字符串的索引
a+= s[suijishu];//将随机产生的四个字符连成一个字符串表示验证码
}
Console.WriteLine("验证码:"+a);
string b = a.ToUpper();//后台执行把输出的字符串全部转换为大写
Console.Write("请输入验证码:");
string r = Console.ReadLine();
string q = r.ToUpper();//将用户输入的字符串全部转换为大写,以便与输出的字符串相比较
if (q == b)//如果用户输入的和电脑输出的相同,则验证成功
{
Console.WriteLine("验证成功!");
}
else//如果不一样,则输入错误,外部也可以加个无限循环,当输入错误时重新输入,成功时if下面加break跳出
{
Console.WriteLine("输入错误!");
}
Console.ReadLine();
}
}
}