這是我的代碼
List<String>?st=new?ArrayList<String>();
String?b="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0123456789";
Random?random=new?Random();
for(int?i=0;i<10;i++){
String?a="";
do{
//首先隨機0~10的數字來確定長度
int?c=random.nextInt(10)+1;
for(int?j=0;j<c;j++){
//隨機生成0-61的數字,取出b字符串里面相對應位置的字符添加到a里面
int?t=random.nextInt(61);
a=a+b.charAt(t);
}
}while(st.contains(a));
st.add(a);
System.out.println("將要添加字符串:"+a);
2015-11-14
while條件好像寫錯了,個人認為應該是while(!st.contains(a))
2015-11-11
有兩個建議:
建議把int?c=random.nextInt(10)+1; ? ? ?改成int c=random.nextInt(9)+1; ? ? ?這樣就不會生成11位的String了。
建議把int?t=random.nextInt(61); ? ? ? 改成int t=random.nextInt(b.length()-1); ? ? 這樣程序更加靈活一些。