java简单验证码生成程序

时间:2024-01-01 19:58:03

下面的函数,返回的字符串就是所需验证码

public String id(){

        Random ra =new Random();
st="";
String [] w= {"0","1","2","3","4","5","6","7","8","9","K","Q","p","m","n","b","w","z","x","g","t","y","f"};
for(int i=0;i<4;i++)
{
st=st+w[ra.nextInt(23)];
}
return st; }

思路就是利用字符串数组设置所需验证码包含的字符种类,然后利用取随机数的方法取出要求个数的随机字符,把它们依次拼接在新的字符串中并返回,这样就实现了随机生成验证码的目的。