创建随机数,且不能重复,用Set集合
//创建随机数,且不能重复,用Set集合
@Test
public void getNumber(){
Random random=new Random();
HashSet<Integer> hashSet=new HashSet<>();
while (hashSet.size()<10){
hashSet.add(random.nextInt(20)+1);
}
for (Integer hs:hashSet) {
System.out.println(hs);
}
}
从键盘输入字符串,去掉重复字符输出
//从键盘输入字符串,去掉重复字符输出
public static void getSingleChar(){
Scanner sc=new Scanner(System.in);
System.out.println("请输入一串字符:");
String str=sc.nextLine();
char[] ch=str.toCharArray();
LinkedHashSet<Character> linkedHashSet=new LinkedHashSet<>();
for (char c:ch) {
linkedHashSet.add(c);
}
for (Character c:linkedHashSet) {
System.out.println(c);
}
}