随机验证码生成

本文介绍了使用Java语言生成随机验证码的三种不同方法,包括利用StringBuilder进行构建、使用空字符串拼接以及生成包含随机字母的验证码。每种方法都附带了详细的代码实现。

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

 1 package com.Stringuse;
 2 
 3 import java.util.Random;
 4  //使用StringBuilder的方发创建验证码
 5 /*public class SuiJiMA {
 6     
 7     public static void main(String[] args) {
 8         
 9         StringBuilder   stringBuilder =  new StringBuilder();
10         Random  random = new Random();
11         for(int i=0; i<4;i++){
12             
13              int x = random.nextInt(10);
14             stringBuilder.append(x);    
15         }
16         System.out.print("验证码:"+stringBuilder);
17     }
18 }*/
19 
20 //   使用空字符串
21 public class SuiJiMA {
22     public static void main(String[] args) {
23         String st = " ";
24         Random random = new Random();
25         for(int i=0;i<4;i++){
26             int x = random.nextInt(10);
27             st = st + x;
28         }
29         System.out.println("验证码:"+st);
30     }
31 }

 1 //生成随机字母验证码
 2 public class SuiJiMA {
 3     public static void main(String[] args) {
 4       StringBuilder stringBuilder = new StringBuilder();
 5       Random random = new Random();
 6       for(int i=0;i<4;i++){
 7           int t = random.nextInt(2);
 8           char ch = t ==0? (char)(random.nextInt(26)+'a'):(char)(random.nextInt(26)+'A');
 9           stringBuilder.append(ch);
10       }
11       System.out.println(stringBuilder);
12     }
13 }

 

转载于:https://www.cnblogs.com/fujilong/p/4689319.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值