根据id生成邀请码,直接上代码:
private static final char chars[] = {'J', '4', 'B', 'X', 'E', 'V', 'F', 'G', 'W', 'I', 'A', 'K',
'7', '5', 'N', 'P', 'Q', 'R', 'S', '3', 'U', '1', 'C', '2', 'T', '6', 'H', 'Y', '8', 'M', 'L', 'D'};
public static String genInviteCode(long id, int len) {
char[] a = new char[len];
for (int i = 0; i < a.length; i++) {
int pow = (int) Math.pow(chars.length, i);
a[i] = charAtStuff((int) (id / pow % chars.length) + i);
}
return String.valueOf(a);
}
private static char charAtStuff(int index) {
return index < chars.length ? chars[index] : chars[index - chars.length];
}
现在的csdn插入代码真方便,样式风格看起来也不错
2.0版:
public static Long genUserId(long id, int len) {
char[] a = new cha