验证码
public static void main(String[] args) {
String code = createCode(5);
System.out.println(code);
}
public static String createCode(int n) {
Random r = new Random();
String code = "";
for (int i = 0; i <n ; i++) {
int type = r.nextInt(3);//生成随机数 0 1 2
switch(type){
case 0:
//大写字母
char ch = (char)(r.nextInt(26)+65);
code+=ch;
break;
case 1:
//小写字母
char CH = (char)(r.nextInt(26)+97);
code+=CH;
break;
case 2:
//大写字母
int num = r.nextInt(10);//0-9
code+=num;
break;
}
}
return code;
}
网友评论