C#生成验证码之四位随机数

本文介绍了一个简单的C#应用程序,用于生成四位数字的验证码并进行用户输入的校验。通过使用随机数生成器来创建验证码,并将其显示在界面上,用户输入验证码后,程序会检查其正确性并给出相应的提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 
11 namespace WindowsFormsApplication2
12 {
13     public partial class Form1 : Form
14     {
15         public Form1()
16         {
17             InitializeComponent();
18         }
19         private void Form1_Load(object sender, EventArgs e)
20         {
21             string vc = "";
22             Random rNum = new Random();//随机生成类
23             int num1 = rNum.Next(0, 9);//返回指定范围内的随机数
24             int num2 = rNum.Next(0, 9);
25             int num3 = rNum.Next(0, 9);
26             int num4 = rNum.Next(0, 9);
27 
28             int[] nums = new int[4] { num1, num2, num3, num4 };
29             for (int i = 0; i < nums.Length; i++)//循环添加四个随机生成数
30             {
31                 vc += nums[i].ToString();
32             }
33             lblVerificationCode.Text = vc;
34         }
35         private void btnVerification_Click(object sender, EventArgs e)
36         {
37             if (txtInput.Text != null && txtInput.Text != "")//用户输入不为空
38             {
39                 if (txtInput.Text == lblVerificationCode.Text)//判断用户输入与随机生成的四位数是否相同
40                 {
41                     MessageBox.Show("验证成功!");
42                 }
43                 else
44                 {
45                     MessageBox.Show("验证失败!");
46                 }
47             }
48             else
49             {
50                 MessageBox.Show("请输入验证码!");
51             }
52         }
53 
54         
55     }
56 }

 

转载于:https://www.cnblogs.com/liupingii/p/7113387.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值