要求如图,我用字符串可以做,用StringBuilder做就不对,生成字符串不够十条,请教下哪里错了。
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Exercise {
void testSort3() {
List<String>stringList = new ArrayList<String>();
for(int i = 0;i < 10;i++) {
Random random = new Random();
int c = random.nextInt(10);
String str = "";
String z;
for(int j = 0 ;j < c; j++) {
do{
char w = (char) (random.nextInt(26) + 65 );//(Math.random() * 27 + 65)
z = String.valueOf(w);
str += z;
}while(stringList.contains(str));
}
System.out.print(str + " ");
}
System.out.println();
}
void testSort4() {
List<String>stringList = new ArrayList<String>();
for(int i = 0;i < 10;i++) {
Random random = new Random();
int c = random.nextInt(10);
StringBuilder sb = new StringBuilder(""); //无参的构造方法也试过,还是不对
String str = "";
String z;
for(int j = 0 ;j < c; j++) {
do{
char w = (char) (random.nextInt(26) + 65 );//(Math.random() * 27 + 65)
z = String.valueOf(w);
sb.append(z);
str = sb.toString();
}while(stringList.contains(str));
}
System.out.print(str + " ");
}
}
public static void main(String [] args) {
CollectionTest ct = new CollectionTest();
ct.testSort3();
ct.testSort4();
}
}
添加回答
举报
0/150
提交
取消