public class CreateCode {
private static String []str_char = {"A","B","C","D","E","F","G","H",
"I","J","K","L","M","N","O","P","Q",
"R","S","T","U","V","W","X","Y","Z"};
private static String one_str_max_num = "9";
private static String two_str_max_num = "9";
private static int str_max_char = 25;
* 邀请码
* @param code
* @return
public static String getInviteCode(String code){
String newCode = "";
if (StringUtils.isEmpty(code)){
newCode = "0A0A";
}else {
StringBuffer sb = new StringBuffer();
String []str_string = code.split("\\d");
String []num_string = code.split("\\D");
for (int i = 0; i < str_string.length; i++) {
if (StringUtils.isEmpty(str_string[i])){
continue;
sb.append(str_string[i]);
if (i != str_string.length -1){
sb.append(",");
str_string = sb.toString().split(",");
int one_char = 0;
int two_char = 0;
for (int j = 0; j < str_char.length; j++) {
if (str_char[j].equals(str_string[0])){
one_char = j;
break;
for (int j = 0; j < str_char.length; j++) {
if (str_char[j].equals(str_string[1])){
two_char = j;
break;
one_str_max_num = "9";
two_str_max_num = "9";
for (int i = 1; i < num_string[0].length(); i++) {
one_str_max_num += "9";
for (int i = 1; i < num_string[1].length(); i++) {
two_str_max_num += "9";
newCode = createCode(num_string[0], one_char, num_string[1], two_char);
return newCode;
* 产生code
* @param one_char
* @param two_char
* @param num_string_one
* @param num_string_two
* @return
private static String createCode(String num_string_one, int one_char, String num_string_two, int two_char){
int one_max_num = Integer.valueOf(one_str_max_num);
int two_max_num = Integer.valueOf(two_str_max_num);
int one_num = Integer.valueOf(num_string_one);
int two_num = Integer.valueOf(num_string_two);
if (two_num < two_max_num){
two_num += 1;
}else if (two_num == two_max_num && two_char < str_max_char){
two_num = 0;
two_char += 1;
}else if (two_num == two_max_num && two_char == str_max_char && one_num < one_max_num){
two_num = 0;
two_char = 0;
one_num += 1;
}else if (two_num == two_max_num && two_char == str_max_char && one_num == one_max_num && one_char < str_max_char){
two_num = 0;
two_char = 0;
one_num = 0;
one_char += 1;
}else if (two_num == two_max_num && two_char == str_max_char
&& one_num == one_max_num && one_char == str_max_char && one_max_num == two_max_num){
two_str_max_num += "9";
two_num = 0;
two_char = 0;
one_num = 0;
one_char = 0;
return createCode(one_num + "", one_char, two_num + "", two_char);
}else if (two_num > two_max_num){
for (int i = 1; i < num_string_two.length(); i++) {
two_str_max_num += "9";
return createCode(one_num + "", one_char, two_num + "" , two_char);
}else if (one_num > one_max_num){
for (int i = 1; i < num_string_one.length(); i++) {
one_str_max_num += "9";
return createCode(one_num + "", one_char, two_num + "", two_char);
}else if (two_num == two_max_num){
if (two_char == str_max_char && one_num < two_num){
one_str_max_num += "9";
one_num = 0;
one_char = 0;
two_num = 0;
two_char = 0;
return createCode(one_num + "", one_char, two_num + "", two_char);
}else if (one_num == one_max_num){
if (one_char == str_max_char){
two_str_max_num += "9";
one_num = 0;
one_char = 0;
two_num = 0;
two_char = 0;
return createCode(one_num + "", one_char,two_num + "", two_char);
String one_num_str = one_num + "";
String two_num_str = two_num + "";
for (int i = 1;i < one_str_max_num.length(); i++) {
if (one_num < Math.pow(10, i)){
one_num_str = "0" + one_num_str;
for (int i = 1;i < two_str_max_num.length(); i++) {
if (two_num < Math.pow(10, i)){
two_num_str = "0" + two_num_str;
String code =one_num_str + str_char[one_char] + two_num_str + str_char[two_char];
return code;
public static void main(String []arg){
String code = "";
for (int i = 0;i < 100; i++) {
code = getInviteCode(code);
System.out.println("第"+i+"#######code#########"+code);
Java生成不重复的邀请码/退换码,数字字母组合/** * 生成邀请码工具类 */public class CreateCode { private static String []str_char = {"A","B","C","D","E","F","G","H", "I","J","K","L","M","N","O","P","Q", "R","S","T","U","V","W","X","Y","Z"}; private
需要通过资料
码
,拿到数据库中的某个文件资源
需要通过
邀请码
,对某个用户做一些操作
这个资料
码
和
邀请码
都是随机
码
,多位,由
数字
和
字母
组成
,可以与整形类型的ID相互转换
网络上找到的实现方法,进行一些简单思考和更改
1. 如何保证不重复性质?
我们发现进制数是不重复的,我们来看一下进制数
比如二进制, 二位二进制数可以表示 2^2个十进制数 [0,3]
在许多APP中,为了推广经营用户常常会有分享功能,APP内的用户拥全局
唯一
的
邀请码
,将
邀请码
分享给自己的好友,好友按照分享的引导步骤填写
邀请码
,邀请人就可以获得一定的奖励,那么我们在开发过程中要如何
生成
一个全局
唯一
的
邀请码
呢,下面笔者提一下自己的思路,如果有更好的解决方案,欢迎各位小伙伴一起交流。
邀请码
的
组成
现在大多数的
邀请码
由
数字
和
字母
组成
,长度由业务需求而定,在此,笔者想要的邀...
eclipse导入项目报 Unknown error: Unable to build: the file dx.jar was not loaded from the SDK folder
FileNotFoundException: /storage/emulated/0/Pictures/1582: open failed: EACCES (Permission denied)