java编程:如何编写随机生成六位由大写和数字组成的验证码,并且I和O不能出现?

2025-05-24 00:51:35
推荐回答(1个)
回答(1):

char[] arr=new char[6];
Random r=new Random();
String s="";
char temp=' ';
for(int i=0;i<6;i++){
int j=r.nextInt(36);
if(j>=26) j=j-26-17;
arr[i]=(char) (65+j);
if(arr[i]=='O'){
if(temp=='I'){
i--;
continue;
}else {
temp='O';
}
}
if(arr[i]=='I'){
if(temp=='O'){
i--;
continue;
}else{
temp='I';
}
}
s+=arr[i];
}
System.out.println(s);