创建无重复的随机整数数组

huangapple go评论79阅读模式
英文:

Creating random integer array without duplicates

问题

public class Fill {
    private static int size;
    private static int maxVal;

    public static void main(String[] args) {

        Scanner kb = new Scanner(System.in);

        // Ask user to enter the size of the array
        System.out.print("Please enter the size of the array: ");
        size = kb.nextInt();

        // Ask user to enter the maximum value allowed
        System.out.print("Please enter the maximum value: ");
        maxVal = kb.nextInt();

        // Call fill() method
        int arr[] = fill(size, maxVal);

        // Print filled array
        System.out.print("[");
        for (int i = 0; i < arr.length - 1; i++)
            System.out.print(arr[i] + ",");
        System.out.print(arr[arr.length - 1] + "]");
    }

    public static int[] fill(int size, int maxVal) {
        int arr[] = new int[size];

        Random random = new Random();

        // Fills the array with random numbers between 0 and maximum value
        if (size <= 0 || maxVal < size - 1) {
            System.out.print("Incorrect Parameters. Please Retry");
            main(null);
        } else {
            for (int j = 0; j < size; j++) {
                arr[j] = random.nextInt(maxVal);
                // Check array for duplicates
                for (int k = j + 1; k < size; k++) {
                    if (arr[j] == arr[k]) {
                        //create new random array
                        fill(size, maxVal);
                    }
                }
            }
        }

        return arr;
    }
}
英文:

I am trying to create a random array without duplicates.

The assignment is to take an integer array and maximum value from user, fills the array with random numbers between 0 and maximum value, and display random array without duplicates, WITHOUT using any other classes except random and scanner.

This is a sample output:

Please enter the size of the array: 10

Please enter the maximum value: 50

[39,2,17,49,12,19,40,31,42,15]

I need help in removing the duplicates. I am not sure if what I am doing is correct, I am a bit of a beginner but here is what I have so far. Help would be greatly appreciated. Thanks.

public class Fill {
private static int size;
private static int maxVal;
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
// Ask user to enter the size of the array
System.out.print(&quot;Please enter the size of the array: &quot;);
size = kb.nextInt();
// Ask user to enter the maximum value allowed
System.out.print(&quot;Please enter the maximum value: &quot;);
maxVal = kb.nextInt();
// Call fill() method
int arr[] = fill(size, maxVal);
// Print filled array
System.out.print(&quot;[&quot;);
for (int i = 0; i &lt; arr.length - 1; i++)
System.out.print(arr[i] + &quot;,&quot;);
System.out.print(arr[arr.length - 1] + &quot;]&quot;);
}
public static int[] fill(int size, int maxVal) {
int arr[] = new int[size];
Random random = new Random();
// Fills the array with random numbers between 0 and maximum value
if (size &lt;= 0 || maxVal &lt; size - 1) {
System.out.print(&quot;Incorrect Parameters. Please Retry&quot;);
main(null);
} else {
for (int j = 0; j &lt; size; j++) {
arr[j] = random.nextInt(maxVal);
// Check array for duplicates
for (int k = j + 1; k &lt; size; k++) {
if(arr[j] == arr[k]) {
//create new random array
fill(size, maxVal);
}
}
}
}
return arr;
}
}

答案1

得分: 1

我已经编辑并修复了你的代码,如下所示:

public class Fill {
    private static int size;
    private static int maxVal;

    public static void main(String[] args) {

        Scanner kb = new Scanner(System.in);

        // 要求用户输入数组的大小
        System.out.print("请输入数组的大小:");
        size = kb.nextInt();

        // 要求用户输入允许的最大值
        System.out.print("请输入最大值:");
        maxVal = kb.nextInt();

        // 调用 fill() 方法
        int arr[] = fill(size, maxVal);

        // 打印填充后的数组
        System.out.print("[");
        for (int i = 0; i < arr.length - 1; i++)
            System.out.print(arr[i] + ",");
        System.out.print(arr[arr.length - 1] + "]");
    }

    public static int[] fill(int size, int maxVal) {
        int arr[] = new int[size];

        Random random = new Random();

        // 使用介于 0 和最大值之间的随机数填充数组
        if (size <= 0 || maxVal < size) {
            System.out.print("参数错误,请重试");
            main(null);
        } else {
            for (int j = 0; j < size; j++) {
                int newNumber = random.nextInt(maxVal + 1);
                // 检查数组中是否已存在重复值

                while (alreadyExist(newNumber, arr)) {
                    newNumber = random.nextInt(maxVal + 1);
                }
                arr[j] = newNumber;
            }
        }

        return arr;
    }

    static boolean alreadyExist(int a, int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == a)
                return true;
        }
        return false;
    }
}

现在它不会返回重复的值了。

英文:

I have edited and fixed some issues in your code as below:

public class Fill {
private static int size;
private static int maxVal;
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
// Ask user to enter the size of the array
System.out.print(&quot;Please enter the size of the array: &quot;);
size = kb.nextInt();
// Ask user to enter the maximum value allowed
System.out.print(&quot;Please enter the maximum value: &quot;);
maxVal = kb.nextInt();
// Call fill() method
int arr[] = fill(size, maxVal);
// Print filled array
System.out.print(&quot;[&quot;);
for (int i = 0; i &lt; arr.length - 1; i++)
System.out.print(arr[i] + &quot;,&quot;);
System.out.print(arr[arr.length - 1] + &quot;]&quot;);
}
public static int[] fill(int size, int maxVal) {
int arr[] = new int[size];
Random random = new Random();
// Fills the array with random numbers between 0 and maximum value
if (size &lt;= 0 || maxVal &lt; size ) {
System.out.print(&quot;Incorrect Parameters. Please Retry&quot;);
main(null);
} else {
for (int j = 0; j &lt; size; j++) {
int newNumber = random.nextInt(maxVal + 1);
// Check array for duplicates
while(alreadyExist(newNumber, arr)){
newNumber = random.nextInt(maxVal + 1);
}
arr[j] = newNumber;
}
}
return arr;
}
static boolean alreadyExist(int a, int[] arr){
for(int i = 0 ; i &lt; arr.length ; i++){
if(arr[i] == a) return true;
}
return false;
}
}

Now it does not return any repetitive value.

huangapple
  • 本文由 发表于 2020年10月10日 12:18:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64289923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定