1 问题
编写程序,创建一个6位随即密码的字典生成器。
2 方法
在Java中,可以从000 000~ZZZ ZZZ~zzz zzz,依次生成6位验证码的集合,一共包含 (10 + 26 + 26)6 个6位验证码
代码清单 1
private static String DICT_RAW_LETTER = ""; static { DICT_RAW_LETTER += "0123456789"; DICT_RAW_LETTER += "abcdefghijklmnopqrstuvwxyz"; DICT_RAW_LETTER += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; } public static void contiGeneSixBitsCodeForLoop() { int len = DICT_RAW_LETTER.length(); String code = ""; for(int a = 0; a < len; a++) { code += DICT_RAW_LETTER.charAt(a); for(int b = 0; b < len; b++) { code += DICT_RAW_LETTER.charAt(b); for(int c = 0; c < len; c++) { code += DICT_RAW_LETTER.charAt(c); for(int d = 0; d < len; d++) { code += DICT_RAW_LETTER.charAt(d); for(int e = 0; e < len; e++) { code += DICT_RAW_LETTER.charAt(e); for(int f = 0; f < len; f++) { code += DICT_RAW_LETTER.charAt(f); // System.out.println(code); code = code.substring(0, code.length() - 1); } code = code.substring(0, code.length() - 1); } code = code.substring(0, code.length() - 1); } code = code.substring(0, code.length() - 1); } code = code.substring(0, code.length() - 1); } code = code.substring(0, code.length() - 1); } } |
3 结语
以上是关于如何使用Java随机生成6位数密码的方法,通过上面的代码,我们可以看到,在 java中有很多种随机生成6位数密码的方法。其实,我们不难发现, java中的很多方法都是以函数作为其核心内容的。其实这也不难理解,因为在 Java中大多数方法都是通过调用函数来实现其功能的。当然,我们也可以将我们常用到的一些方法编写成程序代码,这样会更加方便。