小编Spe*_*edo的帖子

在数组中存储非重复数字

概述:我的程序必须在数组中存储20个非重复的随机生成的数字,范围为1到100.问题:如果我在内部for循环中找到匹配项(重复的#),我会标记布尔变量.这是我不知道该怎么做.在for循环之后,如果没有标记布尔值,我想将随机数添加到数组中.我还想增加x(直到我存储了20个非重复数字),但前提是我将元素添加到数组中.

public void duplication(){
    int max = 100; // max value for range
    int min = 1; // min value for range
    boolean duplicate = false;
    Random rand = new Random();

    for (int x = 0; x < 20; x++){
        //initiates array that stores 20 values
        int[] all = new int[20];
        //generates # from 1-100
        int randomNum = rand.nextInt((max - min) + 1) + min;
        all[x] = randomNum;
        //iterates through array
        for (int i : all) {
            //if there's a …
Run Code Online (Sandbox Code Playgroud)

java arrays random boolean

2
推荐指数
1
解决办法
697
查看次数

标签 统计

arrays ×1

boolean ×1

java ×1

random ×1