将不重复的数字存储在数组中
Storing non-duplicated numbers in an array
概览:
我的程序必须在数组中存储 20 个不重复的随机生成的数字,范围从 1 到 100。
问题:
如果我在内部 for 循环中找到匹配项(重复的#),我会标记布尔变量。这是我不知道该怎么做的。在 for 循环之后,如果布尔值没有被标记,我想将随机数添加到数组中。我还想增加 x(直到我存储了 20 个非重复的数字),但前提是我将元素添加到数组中。
public void duplication(){
int max = 100; // max value for range
int min = 1; // min value for range
boolean duplicate = false;
Random rand = new Random();
for (int x = 0; x < 20; x++){
//initiates array that stores 20 values
int[] all = new int[20];
//generates # from 1-100
int randomNum = rand.nextInt((max - min) + 1) + min;
all[x] = randomNum;
//iterates through array
for (int i : all) {
//if there's a match (duplicate) flag boolean
if (i == randomNum){
duplicate = true;
}
else {
duplicate = false;
}
}
}
//if boolean hasn't been flagged
if (duplicate=false){
//store to array
}
}
这将为您提供一个 0 到 100 之间的随机整数数组
public static void main(String[] args) {
Set<Integer> s = new HashSet<Integer>(); // create a set
Random r = new Random();
while (s.size() < 20) {
s.add(r.nextInt(100)+1); // add elements to set upto size =20
}
Integer[] array = new Integer[20];
s.toArray(array); // convert set to array of Integers
System.out.println(Arrays.toString(array));
}
O/P :
[68, 69, 64, 65, 66, 67, 47, 74, 14, 17, 16, 21, 81, 54, 52, 82, 59, 57, 63, 88]
首先你需要在循环之前初始化你的数组,其次,不要在第二个循环中将重复设置为false,因为你在最后一个数字可以将它设置为false,即使之前有重复。最后一个,只有当你没有找到重复项时才设置值,如果重复项为真,则减小 x 值。
public void duplication(){
int max = 100; // max value for range
int min = 1; // min value for range
boolean duplicate = false;
Random rand = new Random();
//initiates array that stores 20 values
//you need to init it before the loop!
int[] all = new int[20];
for (int x = 0; x < 20; x++){
duplicate = false;
//generates # from 1-100
int randomNum = rand.nextInt((max - min) + 1) + min;
//iterates through array
for (int i : all) {
//if there's a match (duplicate) flag boolean
if (i == randomNum){
duplicate = true;
//we can break the loop here
break;
}
}
//if boolean is true, just stay at the same x value(for loop will increase by 1, thats why we decrese it by 1 here)
if (duplicate){
x--;
} else {
//if everything is ok, set the number
all[x] = randomNum;
}
}
}
您可以使用 while 循环和 for 循环来完成此操作,如下所示。在 while 循环中,您生成一个数字,然后检查重复项。如果不是重复值,则添加到数组并增加索引值。如果它是重复值,则再次 while 循环继续。
public static void duplication() {
int max = 100; // max value for range
int min = 1; // min value for range
Random rand = new Random();
int index = 0; // counter to track 20 numbers
int[] all = new int[20];
while (index < 20) {
boolean duplicate = false;
int randomNum = rand.nextInt((max - min) + 1) + min;
for (int i = 0; i < all.length; i++) {
//if there's a match (duplicate) flag boolean
if (all[i] == randomNum) {
duplicate = true;
break;
}
}
if (!duplicate) {
all[index++] = randomNum;
}
}
}
概览: 我的程序必须在数组中存储 20 个不重复的随机生成的数字,范围从 1 到 100。 问题: 如果我在内部 for 循环中找到匹配项(重复的#),我会标记布尔变量。这是我不知道该怎么做的。在 for 循环之后,如果布尔值没有被标记,我想将随机数添加到数组中。我还想增加 x(直到我存储了 20 个非重复的数字),但前提是我将元素添加到数组中。
public void duplication(){
int max = 100; // max value for range
int min = 1; // min value for range
boolean duplicate = false;
Random rand = new Random();
for (int x = 0; x < 20; x++){
//initiates array that stores 20 values
int[] all = new int[20];
//generates # from 1-100
int randomNum = rand.nextInt((max - min) + 1) + min;
all[x] = randomNum;
//iterates through array
for (int i : all) {
//if there's a match (duplicate) flag boolean
if (i == randomNum){
duplicate = true;
}
else {
duplicate = false;
}
}
}
//if boolean hasn't been flagged
if (duplicate=false){
//store to array
}
}
这将为您提供一个 0 到 100 之间的随机整数数组
public static void main(String[] args) {
Set<Integer> s = new HashSet<Integer>(); // create a set
Random r = new Random();
while (s.size() < 20) {
s.add(r.nextInt(100)+1); // add elements to set upto size =20
}
Integer[] array = new Integer[20];
s.toArray(array); // convert set to array of Integers
System.out.println(Arrays.toString(array));
}
O/P :
[68, 69, 64, 65, 66, 67, 47, 74, 14, 17, 16, 21, 81, 54, 52, 82, 59, 57, 63, 88]
首先你需要在循环之前初始化你的数组,其次,不要在第二个循环中将重复设置为false,因为你在最后一个数字可以将它设置为false,即使之前有重复。最后一个,只有当你没有找到重复项时才设置值,如果重复项为真,则减小 x 值。
public void duplication(){
int max = 100; // max value for range
int min = 1; // min value for range
boolean duplicate = false;
Random rand = new Random();
//initiates array that stores 20 values
//you need to init it before the loop!
int[] all = new int[20];
for (int x = 0; x < 20; x++){
duplicate = false;
//generates # from 1-100
int randomNum = rand.nextInt((max - min) + 1) + min;
//iterates through array
for (int i : all) {
//if there's a match (duplicate) flag boolean
if (i == randomNum){
duplicate = true;
//we can break the loop here
break;
}
}
//if boolean is true, just stay at the same x value(for loop will increase by 1, thats why we decrese it by 1 here)
if (duplicate){
x--;
} else {
//if everything is ok, set the number
all[x] = randomNum;
}
}
}
您可以使用 while 循环和 for 循环来完成此操作,如下所示。在 while 循环中,您生成一个数字,然后检查重复项。如果不是重复值,则添加到数组并增加索引值。如果它是重复值,则再次 while 循环继续。
public static void duplication() {
int max = 100; // max value for range
int min = 1; // min value for range
Random rand = new Random();
int index = 0; // counter to track 20 numbers
int[] all = new int[20];
while (index < 20) {
boolean duplicate = false;
int randomNum = rand.nextInt((max - min) + 1) + min;
for (int i = 0; i < all.length; i++) {
//if there's a match (duplicate) flag boolean
if (all[i] == randomNum) {
duplicate = true;
break;
}
}
if (!duplicate) {
all[index++] = randomNum;
}
}
}