- class TestRandom {
- public static void main(String []args) {
- System.out.println(validate());
- }
- public static char getUpperCase() {
- return (char)((int)(Math.random()*26+65));
- }
- //A-Z
- public static char getLowerCase() {
- return (char)((int)(Math.random()*26+97));
- }
- //a-z
- public static int getNum() {
- return (int)(Math.random()*10);
- }
- public static int getPos() {
- return (int)(Math.random()*3);
- }
- public static String validate() {
- String str="";
- for(int i=0;i<4;i++) {
- int z=getPos();
- switch(z) {
- case 0:
- str+=getUpperCase();
- break;
- case 1:
- str+=getLowerCase();
- default:
- str+=getNum();
- }
- }
- return str;
- }
- }
复制代码 |