您的位置:首页 > 其它

利用随机数生成方法产生激活码,形如12HJ-9Hjf-HHTY-KK8G

2015-01-13 19:03 417 查看
产生一个激活码

package jihuoma;
//产生激活码:类似于2Hs0-ssKj-9HHo-A55D
public class Test {
	public static void main(String[] args) {
		String str="";
		for (int i = 1; i <= 16; i++) {
			int a=(int)(Math.random()*3);//0,1,2,[0,3)之间取整数
			if(a==0)
			{
				str=str+(char)((int)(Math.random()*('Z'-'A'+1))+'A');
			}
			if(a==1)
			{
				str=str+(char)((int)(Math.random()*('z'-'a'+1))+'a');
			}
			if(a==2)
			{
				str=str+(char)((int)(Math.random()*('9'-'0'+1))+'0');
			}
			
			if(i%4==0&&i<16)//每4个之间加一个-代表分开,等于16时就不用再加上-了。
			{
				str=str+'-';
			}
		}
		System.out.println(str);//打印输出随机产生的激活码
	}

}


产生十个激活码(外面多套一层循环)

package jihuoma;
//产生10个激活码
public class Test2 {
	public static void main(String[] args) {
		for (int j = 0; j < 10; j++) {//循环10次,产生10个激活码
			String str="";
			for (int i = 1; i <= 16; i++) {//循环完16次后产生一个激活码
				int a=(int)(Math.random()*3);//0,1,2
				if(a==0)
				{
					str=str+(char)((int)(Math.random()*('Z'-'A'+1))+'A');
				}
				if(a==1)
				{
					str=str+(char)((int)(Math.random()*('z'-'a'+1))+'a');
				}
				if(a==2)
				{
					str=str+(char)((int)(Math.random()*('9'-'0'+1))+'0');
				}
				
				if(i%4==0&&i<16)
				{
					str=str+'-';
				}
			}
			System.out.println(str);
		}
		
	}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: