用java生成6为验证码,要求大小写,数字,不能重复字符

2024-12-14 03:03:21
推荐回答(1个)
回答1:

public class test{
private static String [] str={"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","0","1","2","3","4","5","6","7","8","9"};
private static String DarwCode(){
String strRandom="";
char temp;
int [] num = new int[6];
int numA=0,numB=0,numC=0,ramNum=(int)(Math.random()*62);
//去掉重复的关键代码
num[0]=ramNum;
for (int i = 1; i < num.length; i++) {
num[i] = (int)(Math.random()*62);
for (int j = 0; j < i; j++) {
while ((num[i]==num[j])&&(i>=1)) {
i--;
}
}
}
//输出字符串
for (int i = 0; i < num.length; i++) {
strRandom+=str[num[i]];
}
for (int i = 0; i temp=strRandom.substring(i,i+1).charAt(0);
if ((int)temp>=48&&(int)temp<=57) {
numA++;
}else if ((int)temp>=65&&(int)temp<=90) {
numB++;
}else if((int)temp>=97&&(int)temp<=122){
numC++;
}
}
if ((numA+numB==6)||(numA+numC==6)||(numB+numC==6)) {
strRandom = DarwCode();
}
return strRandom;
}
public static void main(String[] args) {
System.out.println(DarwCode());
}
}