簡體   English   中英

如何在隨機生成的數字數組中打印出重復項的第一個實例?

[英]How do I print out the first instance of duplicates in an array of randomly generated numbers?

我將10個隨機數存儲在一個數組中,並將其打印在20-50范圍內。 我無法找到重復的作品。 如果數組中有一個實例,發現有重復項,我將嘗試打印出找到它的位置,而不是下標。

例如:

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

您必須在for循環之外聲明ij才能使該方法起作用。 另外,如果要打印數組中基於1的位置,則可以打印numbers[i+1]numbers[j+1] ,我猜應該只是i+1j+1

幾個問題:

  • 您的if檢查是否在循環之外找到重復項。 你應該把它們帶進去。

  • 一旦設置了plicateFound布爾值,就永遠不會重置它。 所以你只會得到一場比賽。 因此,您可能還需要重置它。

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

還有進一步的改進范圍,例如您不需要plicateFound變量,也不需要進一步檢查,即是否需要進一步設置和重置變量。

您需要更改的是顯示數字而不是索引。 因此,對於您的第一個循環,請更改為number [i]而不是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);

}

}

您需要使用數字[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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM