練習代碼輸出不了東西,哪里錯了
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionsTest1 {
public void Testsort(){
List<String>stringList=new ArrayList<String>();
String str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqustuvwxyz0123456789";
StringBuilder C=new StringBuilder();
for(int i=0;i<10;i++){
Random random=new Random();
int A=random.nextInt(10);
if(A==0){
continue;
}else{for(int B=0;B<A;B++){
int num=random.nextInt(str.length());
C.append(str.charAt(num));
}
}
while(stringList.contains(C.toString())){
stringList.add(C.toString());
System.out.println("-------排序前-------");
for(String string:stringList){
System.out.println("元素:"+string);
}
System.out.println("-------排序后-------");
Collections.sort(stringList);
for (String string : stringList) {
System.out.println("元素:"+string);
}
}
}
}
public static void main(String[] args) {
// TODO 自動生成的方法存根
CollectionsTest1 ct=new CollectionsTest1();
ct.Testsort();?
}
}
2017-04-25
你的for循環里的if else 寫好復雜的,而且根本沒有起到作用,首先,改成for(int i=0:i<(A=0)?1:A;i++),然后你的str賦值大小寫字母和數字,我覺得這樣太局限了,你應該用阿克斯碼更好,然后用阿克斯碼轉換成字符賦予字符串,就好了