请设置一个随机验证码,由数字,大写及小写字母组成。
package 验证码;
import java.util.Random;
public class DEMO {
public static void main(String[] args) {
System.out.println(createnode(5));
}
public static String createnode(int n)
{
Random r=new Random();
String code=""; //字符串相加
for( int i=1;i<=n;i++) {
//0,1,2,3.......n
// 随机一个验证码
//type取0,1,2 0为0~9的随机数字 1,2为大写和小写字母
int type = r.nextInt(3);
switch (type) {
case 0:
code+= r.nextInt(9);
break;
case 1:
char ch1= (char)(r.nextInt(26)+65); //重点 数据类型转换 ASII码
code+=ch1;
break;
case 2:
char ch2=(char)(r.nextInt(26)+97);
code+=ch2;
break;
}
}return code;
}
}