package com.lovo.classes;
import java.util.Random;
import java.util.TreeSet;
import java.util.Scanner;
public class day5test4 {
public static void main(String[] args) {
// 调用TreeSet容器输出十个1-100的不重复数据
Random r = new Random();
TreeSet<Integer> hs = new TreeSet();
int count = 0;
while (hs.size()<10){
int num = r.nextInt(100)+1;
if (num>=0&&num<=10)
hs.add(num);
}
for (Integer i:hs)
System.out.print(i+" ");
/**
* 随机获取十个数据,数组里面最多只有2个相同数据
*/
int[] arr = new int[10];
int index = 0;//用于记录满足要求的数据个数是否达到十个
while (true){
int times = 0;//相同随机数不能超过2个
int number = (int)(Math.random()*10+1);//1-10
for (int i=0;i<array.length;i++){
if (number == arr[i])
times++;
}
if (times >= 2)
continue;
arr[index] = number;
index++;
if (index == arr.length-1)
break;
}
for (int i : arr)
System.out.print(i+",");
/**
* 随机输入十个数,实现数组里面最多只有2个相同数据
*/
int [] array = new int[10];
int countN = 0;
for(int i=0; i<array.length; i++){
int number = (int)(Math.random()*10+1);//获取随机数
for(int j = 0; j<array.length; j++){
if(number == array[j]){
countN++;
}
}
if(countN >=2){
i = i-1;
countN = 0;
}
else{
array[i] = number;
}
}
for (int i : array)
System.out.print(i+",");
}
}