为了账号安全,请及时绑定邮箱和手机立即绑定

java产生随机验证码,有惑

java产生随机验证码,有惑

小唯快跑啊 2019-05-21 10:47:49
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;i
查看完整描述

2 回答

?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

具体是哪一行看不懂呢?这个就是声明了一个验证码可取值数组CHARScharFlags的每个值对应了每个char是否已经用到过。(这里的目的是生成的验证码不会出现重复字符比如AA1234,两个A重复了)verifyCodes是最终生成的验证码数组。代码逻辑翻译过来就是:每次从CHARS里面取一个字符,看这个字符是否已经用过,没有用过就放到最终的验证码数组verifyCodes中。然后取下一个,如果从CHARS中取出的字符已经用过,则再随机生成一个下标,重新取一个字符。直到取到6个不重复的字符为止。
                            
查看完整回答
反对 回复 2019-05-21
  • 2 回答
  • 0 关注
  • 332 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信