---------------------------------------------------------------------------------------------------- public class StringLen {
public static void main(String[] args) { StringBuffer buff = new StringBuffer(); int len = 10; if (len < 3) { // 必须包含大小写字母和数字 // 那这个字符串最小长度是3. return; }
int index = 0; for (int i = 0; i < len; i++) { char c = 'a'; int random = (int) (Math.random() * 1000); if (i > 3) { index = random % 3; } else { index = i; }
switch (index) { case 0: buff.append((char) (97 + random % 26)); break; case 1: buff.append((char) (65 + random % 26)); break; case 2: buff.append((char) (48 + random % 10)); break; } }