【转】java验证码算法

1.生成四位字符、整数组成的验证码(无重复码验证)
package week1;
import java.util.Random;
import org.omg.IOP.Codec;
public class doWhile{
public static void main(String[] args){
for(int k=0;k<100;k++){
char[]chr1=new char[4];
checkcode(chr1);
System.out.println(chr1);}
}
public static char[]checkcode(char[] chr1){
char[] chr = "0123456789abcdefhijkmnpqrstuvwxyABCDEFGHJKLMNPQRSTUVWXY".toCharArray();
Random r=new Random();
int n=chr.length;
boolean[] used=new boolean[n];
int j=0;
while(true){
int index=r.nextInt(n);
if(used[index]==true) continue;
chr1[j]=chr[index];
used[index]=true;
j++;
if(j>=4)break;
}
return chr1;

}
}

2.生成四位字符、整数组成的验证码并且相邻码不重复
package week1;

import java.util.Random;


public class doWhile{
public static void main(String[] args){
for(int k=0;k<100;k++){
char[]chr1=new char[4];
checkcode(chr1);
System.out.println(chr1);}
}
public static char[]checkcode(char[] chr1){
char[] chr = "0123456789abcdefhijkmnpqrstuvwxyABCDEFGHJKLMNPQRSTUVWXY".toCharArray();
Random r=new Random();
int n=chr.length;
boolean[] used=new boolean[n];
int j=0;
while(true){
int index=r.nextInt(n);
if(used[index]==true) continue;
chr1[j]=chr[index];
used[index]=true;
j++;
if(j>=4)break;
}
return chr1;


}
}

你可能感兴趣的:(java)