日期:2014-05-20 浏览次数:20707 次
package com.ricky.www; public class Test { public String getPassword() { int index = 0; String[] p_array = new String[62]; /** * 填充数组 */ for (int i = 0; i <= 9; i++) { p_array[index++] = i + ""; } for (int i = 'A'; i <= 'Z'; i++) { char c = (char) i; p_array[index++] = new String(c + ""); } for (int i = 'a'; i <= 'z'; i++) { char c = (char) i; p_array[index++] = new String(c + ""); } /** * 返回6位的随机密码 */ StringBuffer password = new StringBuffer(); for (int i = 0; i < 6; i++) { int random =(int)(Math.random() * 62); password.append(p_array[random]); } if (rule(password.toString())) { return password.toString(); } else { return getPassword(); } } private boolean rule(String password) { char[] p_array = password.toCharArray(); // 因为1 + 2 + 3 + 4 +5 +6 = 1 * 6 + 15;类推 int total = p_array[0] * 6 + 15; int sum = 0; for (int i = 0; i < 6; i++) { sum += p_array[i]; } if (sum == total) { return false; } return true; } public static void main(String[] args) { String password = new Test().getPassword(); System.out.print(password); } }