问题描述
将[1, 100] 随机添加到 array[100] 中
思路解析
定义value[100] 数组,存储已经赋值的随机数,每次赋值前进行判断,过滤掉已经赋值过的随机数
package com.wy;
/**
* @author HelloWorld
* @create 2021-04-20-9:57
* @email 154803771@qq.com
*/
public class Test {
public static void main(String[] args) {
int[] array = new int[100];
int[] value = new int[100];
int data;
for (int i = 0; i < 100; ) {
data = 1 + (int)(Math.random() * 100);
if (value[data - 1] == 0) {
array[i] = data;
i++;
value[data - 1]++;
}
}
for (int i = 0; i < 100; i++) {
if ((i + 1) % 20 != 0) {
System.out.print(array[i] + "\t");
} else {
System.out.println(array[i]);
}
}
}
}