[英]How do I print out the first instance of duplicates in an array of randomly generated numbers?
I've printed 10 random numbers in the range 20-50 while storing them in an array. 我将10个随机数存储在一个数组中,并将其打印在20-50范围内。 I'm having trouble getting the duplicates found to work. 我无法找到重复的作品。 If there's an instance in the array where there's a duplicate found, I'm trying to print out the position where it was found, not the subscript. 如果数组中有一个实例,发现有重复项,我将尝试打印出找到它的位置,而不是下标。
For example: 例如:
46 46
24 24
46 46
48 48
44 44
20 20
24 24
46 46
44 44
27 27
First pair of duplicates were found at positions: 1 and 3 在以下位置发现第一对重复项:1和3
Is the output that I'm trying to achieve if there's duplicates and then the same except "No duplicates were generated." 如果存在重复项,然后除了“未生成任何重复项”之外,其余的都是我要达到的输出。 if there wasn't any. 如果没有的话。
import javax.swing.JOptionPane;
public class sheet11t1
{
public static void main(String[] args)
{
String results = "";
int numbers[] = new int[10];
int j;
for(int i = 0; i < numbers.length; i++)
{
numbers[i] = (int) ((Math.random() * 31) + 20);
results += i + "\n";
}
boolean duplicateFound = false;
for(int i = 0; i < numbers.length - 1 && !duplicateFound; i++)
{
for(j = i + 1; j < numbers.length && !duplicateFound; j++)
{
if(numbers[i] == numbers[j])
duplicateFound = true;
}
}
if(duplicateFound)
results += "First pair of duplicates were found at positions: " + numbers[i + 1] + " and " + numbers[j + 1];
else
results += "No duplicates were generated.";
JOptionPane.showMessageDialog(null, results);
}
}
You have to declare both i
and j
outside your for loops for that approach to work. 您必须在for循环之外声明i
和j
才能使该方法起作用。 Also, you print numbers[i+1]
and numbers[j+1]
, which i guess should be just i+1
and j+1
if you want to print the 1-based positions in the array. 另外,如果要打印数组中基于1的位置,则可以打印numbers[i+1]
和numbers[j+1]
,我猜应该只是i+1
和j+1
。
Couple of issues: 几个问题:
Your if check for whether duplicate is found or not is outside the loop. 您的if检查是否在循环之外找到重复项。 You should bring them inside. 你应该把它们带进去。
Once you set duplicateFound boolean, you never reset it. 一旦设置了plicateFound布尔值,就永远不会重置它。 So you would just get one match only. 所以你只会得到一场比赛。 So you might need to reset that as well. 因此,您可能还需要重置它。
You are using number[i+1 or j+1], which should be i+1 or j+1 in your result string formation. 您正在使用number [i + 1或j + 1],在结果字符串格式中应为i + 1或j + 1。
for (int i = 0; i < numbers.length - 1 && !duplicateFound; i++) { for (j = i + 1; j < numbers.length && !duplicateFound; j++) { if (numbers[i] == numbers[j]) { duplicateFound = true; } if (duplicateFound) { results += "pair of duplicates were found at positions: " + (i + 1) + " and " + (j + 1) + "\\n"; duplicateFound = false; } } } if (results.isEmpty()) { System.out.println("No duplicates were generated."); } else { System.out.println(results); }
There are further scope of improvement like you don't need duplicateFound variable and no further checks ie if further setting and resetting variable. 还有进一步的改进范围,例如您不需要plicateFound变量,也不需要进一步检查,即是否需要进一步设置和重置变量。
What you have to change is the showing of the numbers instead of the index. 您需要更改的是显示数字而不是索引。 so for your first loop, change to numbers[i] instead of just i. 因此,对于您的第一个循环,请更改为number [i]而不是i。 secondly, In your case of exiting the loop, the i and j are +1, thus when you want to print the index, you just need to specify i and j. 其次,在退出循环的情况下,i和j为+1,因此当您要打印索引时,只需指定i和j。 below is the codes. 下面是代码。
import javax.swing.JOptionPane;
public class example {
public static void main(String[] args) {
String results = "";
int numbers[] = new int[10];
int j = 0;
int i;
for(i = 0; i < numbers.length; i++)
{
numbers[i] = (int) ((Math.random() * 31) + 20);
results += numbers[i] + "\n";
}
boolean duplicateFound = false;
for(i = 0; i < numbers.length - 1 && !duplicateFound; i++)
{
for(j = i + 1; j < numbers.length && !duplicateFound; j++)
{
if(numbers[i] == numbers[j])
duplicateFound = true;
}
}
if(duplicateFound)
results += "First pair of duplicates were found at positions: " + (i) + " and " + (j);
else
results += "No duplicates were generated.";
JOptionPane.showMessageDialog(null, results);
}
}
you need to change printing numbers using numbers[i + 1], numbers[j + 1] to i & j values 您需要使用数字[i + 1],数字[j + 1]更改为i和j值的打印数字
public static void main(String[] args) {
String results = "";
int numbers[] = new int[10];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = (int) ((Math.random() * 31) + 20);
results += (i + 1) + " --- " + numbers[i] + "\n";
}
boolean duplicateFound = false;
int i = 0;
int j = 0;
for (; i < numbers.length - 1 && !duplicateFound; i++) {
for (j = i + 1; j < numbers.length && !duplicateFound; j++) {
if (numbers[i] == numbers[j]) {
duplicateFound = true;
}
}
}
if (duplicateFound) {
results += "First pair of duplicates were found at positions: " + i + " and " + j;
} else {
results += "No duplicates were generated.";
}
JOptionPane.showMessageDialog(null, results);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.