隨機生成6位包含數字大小寫字母的驗證碼

package week;
/**
* 26爲字母
* 十二位數字
*
*/
public class AllCharacter {
public static char[] charArray() {
int i = 1234567890;
String s = "qwertyuiopasdfghjklzxcvbnm";
String S = s.toUpperCase();
String word = s+S+i;
char[] c =word.toCharArray();
return c;
}java

}數組

-----------------------------------------------dom

測試類測試

package week;ui

import java.util.Random;code

public class Test3 {
public static void main(String[] args) {
for(int i=0; i<6;i++) {
String s = verifyCode();
System.out.println(s);
}
}
public static String verifyCode() {
char[] c = AllCharacter.charArray();
Random rd = new Random();
String code="";
for (int k = 0; k < 6; k++) {
int index = rd.nextInt(c.length);//隨機獲取數組長度做爲索引
code+=c[index];//循環添加到字符串後面
}
return code;
}blog

 

}索引

相關文章
相關標籤/搜索