java - java中具有不同出现规则的从一个数组到另一个数组的随机数

标签 java arrays random find-occurrences

我在学校练习时需要帮助。

我需要使用以下数组中的 6 个随机整数创建一个数组:montab[] = {1,2,3,4,5,6,7,8,9,10,25,50,75,100} 并遵循以下规则:

  1. 数字25,50,75,100在数组中只能出现一次
  2. 数字 110 在数组中只能出现两次

我现在尝试了第一条规则,但在极少数情况下,我仍然多次收到该号码。

这是我的代码:

public class Exo7bis {
public static void main (String[] args){
    Random random = new Random();
    int montab[] = {1,2,3,4,5,6,7,8,9,10,25,50,75,100};
    int[] ar1 = new int[6];
    int j = 0, compteur25 = 0, compteur50 = 0, compteur75 = 0, compteur100 = 0;
        for (int i = 0; i < ar1.length; i++) {
            ar1[i] = (montab[new Random().nextInt(montab.length)]);
            if (ar1[i] == 25) {
                compteur25++;
                if (compteur25 > 1) {
                    while (ar1[i] == 25)
                        ar1[i] = (montab[new Random().nextInt(montab.length)]);
                }
            }
            if (ar1[i] == 50) {
                compteur50++;
                if (compteur50 > 1) {
                    while (ar1[i] == 50)
                        ar1[i] = (montab[new Random().nextInt(montab.length)]);
                }
            }
            if (ar1[i] == 75) {
                compteur75++;
                if (compteur75 > 1) {
                    while (ar1[i] == 75)
                        ar1[i] = (montab[new Random().nextInt(montab.length)]);
                }
            }
            if (ar1[i] == 100) {
                compteur100++;
                if (compteur100 > 1) {
                    while (ar1[i] == 100)
                        ar1[i] = (montab[new Random().nextInt(montab.length)]);
                }
            }
        }

        for (int i = 0; i < ar1.length; i++) {
            System.out.print(ar1[i] +" ⎢ " + "\t");
        }
    }
}

我知道我的测试并不完全正确,我发现了问题,但找不到正确的解决方案。

如果有人可以帮助我或给我建议,那就太好了。

提前致谢!

杰里米

最佳答案

有很多方法可以实现此目的,但由于您只使用数组,我建议您创建一个函数来计算 1-10 重复的次数。对于第一个条件,您可以将元素替换为 0,这样下次就不会重复。我认为在代码中很容易解释,所以看看您可以在代码中更改哪些内容:

public static void main(String[] args) {
        Random random = new Random();
        int montab[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25, 50, 75, 100 };
        int[] ar1 = new int[6];
        int j = 0, count = 0;
        while (j < ar1.length) {
            count = 0;
            int index = random.nextInt(montab.length);
            int num = montab[index];
            if (num >= 25) { //adds any number greater or equal to 25
                ar1[j] = num;
                j++;
                montab[index] = 0; // replace the origianl array with 0.
            } else if (num != 0) {
                if(!isRepeated(ar1,num)){ //checks if the array has more than two of the number.
                ar1[j] = num;
                j++;
                }
            }
        }
        for (int i = 0; i < ar1.length; i++) {
            System.out.print(ar1[i] + " ⎢ " + "\t");
        }
    }

    public static boolean isRepeated(int[] arr, int num) { //method that verifies if the array has a number repeated twice or not.
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == num)
                count++;
        }
        return count==2 ? true : false;
    }

我还没有测试过,但我很确定它会起作用!!

关于java - java中具有不同出现规则的从一个数组到另一个数组的随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40773460/

相关文章:

java - Jboss 管理控制台在子系统下不显示任何消息传递选项

java - 独特的随机整数生成器

javascript - 如何使用 SpringBoot 和 Angular2 设置结构?

java - 如何确定文件是否在 File 类型的变量中?

java - JAXB 模式在哪里发布?

c# - 查找双数组中的最大数

javascript - 如何将数组中的已排序数组合并为第三个已排序数组

java - 在java中对数组的输入设置一个范围

java - 如何使用 double 格式的数字为范围之间的随机间隔编写代码?

android - 在 oncreate 方法之外生成一个随机数