import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
// " 第三题:分析以下需求,并用代码实现
// " 1.产生10个1-20之间的随机数要求随机数不能重复
// " 2.产生10个长度为10的不能重复的字符串(里面只能出现大写字母、小写字母、0-9的数字),并遍历打印输出
public class Prctice03 {
public static void main(String[] args) {
Random r = new Random();
HashSet<Integer> hs = new HashSet();
while (hs.size() < 10) {
int Num = r.nextInt(20) + 1;
hs.add(Num);
}
System.out.println(hs);
System.out.println("=========================================================================");
ArrayList<Character> list = new ArrayList<>();
for (int i = '0'; i <= '9'; i++) {
char a = (char) (i);
list.add(a);
}
for (int i = 'a'; i <= 'z'; i++) {
char a = (char) (i);
list.add(a);
}
for (int i = 'A'; i <= 'Z'; i++) {
char a = (char) (i);
list.add(a);
}
for (int i = 0; i < 10; i++) {
HashSet<Character> hs2 = new HashSet();
while (hs2.size() < 10) {
int idex = r.nextInt(26 * 2 + 10);
hs2.add(list.get(idex));
}
Object[] objects = hs2.toArray();
String s=new String();
for (int i1 = 0; i1 < objects.length; i1++) {
s+=objects[i1];
}
System.out.println(s);
}
}
}
|