乐趣区

关于java:账户初始化密码

public class RandomPwd {
    /**
 * 生成随机明码:6 位数字 */ public static String randomSixDigitsPwd() {Random random = new Random();
        char[] arr = new char[6];
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {int randomInt = random.nextInt(10);
            arr[i] = (char)('0' + randomInt); // much better
 sb.append(randomInt);
        }
        System.out.println(new String(arr));
        System.out.println(sb.toString());
        return new String(arr);
    }
    public static void main(String[] args) { }
    public static String complicatedPwd() {Random random = new Random();
        char[] arr = new char[8];
        arr[nextIndex(random, arr)] = nextSpecial(random);
        arr[nextIndex(random, arr)] = nextUpper(random);
        arr[nextIndex(random, arr)] = nextLower(random);
        arr[nextIndex(random, arr)] = nextDigit(random);
        for (int i = 0; i < 8; i++) {if (arr[i] == 0) {arr[i] = nextChar(random);
            }
        }
        System.out.println(new String(arr));
        return new String(arr);
    }
    public static char nextDigit(Random random) {return (char)('0' + random.nextInt(10));
    }
    public static char nextUpper(Random random) {return (char)('A' + random.nextInt(26));
    }
    public static char nextLower(Random random) {return (char)('a' + random.nextInt(26));
    }
    public  static char nextSpecial(Random random) {return specialChars.charAt(random.nextInt(specialChars.length()));
    }
    public static int nextIndex(Random random, char[] arr) {int index = random.nextInt(arr.length);
        while (arr[index] != 0) {index = random.nextInt(arr.length);
        }
        return index;
    }
    /**
 * 生成随机明码:简单 8 位 * 至多要含一个大写字段、一个小写字段、一个特殊符号、一个数字 */
 public static char nextChar(Random random) {switch (random.nextInt(4)) {case 0: return (char)('A' + random.nextInt(26));
            case 1: return (char)('a' + random.nextInt(26));
            case 2: return (char)('0' + random.nextInt(10));
            default: return specialChars.charAt(random.nextInt(specialChars.length()));
        }
    }
    private static final String specialChars = "!@#$%^&*()_+-=";
    /**
 * 生成随机明码:简略 8 位 * 字符可能由大写字母、小写字母、数字和特殊符号组织 */ public static String randomEightPwd() {Random random = new Random();
        char[] arr = new char[8];
        for (int i = 0; i < arr.length; i++) {arr[i] = nextChar(random);
        }
        System.out.println(new String(arr));
        return new String(arr);
    }
}
退出移动版