如何在随机生成的数字数组中打印出第一个重复项实例?

时间:2014-11-10 12:04:49

标签: java arrays loops joptionpane

我在20-50的范围内打印了10个随机数,同时将它们存储在一个数组中。我找不到重复的工作有困难。如果数组中有一个发现重复的实例,我会尝试打印出找到它的位置,而不是下标。

例如:

46

24

46

48

44

20

24

46

44

27

在位置:1和3

处发现第一对重复

如果有重复的话,我试图实现的输出是否是相同的,除了"没有生成重复项。"如果没有。

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);
    }
}

4 个答案:

答案 0 :(得分:0)

您必须在for循环之外声明ij才能使用该方法。此外,如果要在数组中打印基于1的位置,请打印numbers[i+1]numbers[j+1],我想这应该只是i+1j+1

答案 1 :(得分:0)

几个问题:

  • 您是否检查是否发现重复是在循环之外。你应该把它们带进去。

  • 一旦设置了duplicateFound布尔值,就永远不会重置它。所以你只会得到一场比赛。所以你可能也需要重置它。

  • 您正在使用数字[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);
    }
    

还有进一步的改进范围,例如您不需要duplicateFound变量,也无需进一步检查,即是否进一步设置和重置变量。

答案 2 :(得分:0)

您需要更改的是显示数字而不是索引。所以对于你的第一个循环,改为数字[i]而不仅仅是我。 其次,在你退出循环的情况下,i和j是+1,因此当你想要打印索引时,你只需要指定i和j。 以下是代码。

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);

}

}

答案 3 :(得分:0)

您需要使用数字[i + 1],数字[j + 1]更改为i&amp; 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);
}