java产生随机验证码,有惑

importjava.util.Arrays;
importjava.util.Random;
publicclassVerificationCode{
publicstaticvoidmain(String[]args){
String[]CHARS={"3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","J","K","L",
"M","N","P","Q","R","S","T","U","V","W","X","Y"};
boolean[]charFlags=newboolean[CHARS.length];
String[]verifyCodes=newString[6];
Randomran=newRandom();
for(inti=0;iintindex;
do{
index=ran.nextInt(CHARS.length);
}while(charFlags[index]);
verifyCodes[i]=CHARS[index];
charFlags[index]=true;
}
//这段代码求解释看不懂
System.out.print("验证码为:");
for(inti=0;iSystem.out.print(verifyCodes[i]);
}
}
}
小唯快跑啊
浏览 335回答 2
2回答

吃鸡游戏

具体是哪一行看不懂呢?这个就是声明了一个验证码可取值数组CHARScharFlags的每个值对应了每个char是否已经用到过。(这里的目的是生成的验证码不会出现重复字符比如AA1234,两个A重复了)verifyCodes是最终生成的验证码数组。代码逻辑翻译过来就是:每次从CHARS里面取一个字符,看这个字符是否已经用过,没有用过就放到最终的验证码数组verifyCodes中。然后取下一个,如果从CHARS中取出的字符已经用过,则再随机生成一个下标,重新取一个字符。直到取到6个不重复的字符为止。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript