为什么我的代码生成带有重复数字的数字系列?

问题描述 投票:0回答:2

我的代码应该回答以下问题:

Write a Java program to create and display a unique three-digit number using 1, 2, 3, 4. Also count how many three-digit numbers are there.
//        Expected Output
//
//        123
//        124
//        ...
//
//        431
//        432
//        Total number of the three-digit-number is 24"

现在,我使用了

Sets 
if statements
形式的两个检查点。首先,我检查

Random rng = new Random(); String digit = Integer.toString(rng.nextInt(4) + 1);

与以下代码行中

index
rngNumString
处的值不匹配:

if (index == 0 )
    rngNumString[index] = digit;
else if (index == 1 && (!rngNumString[index].equals(digit) && (!rngNumString[index - 1].equals(digit))))
    rngNumString[index] = digit;
else if (index == 2 && (!rngNumString[index].equals(digit)) && (!rngNumString[index - 1].equals(digit)) && (!rngNumString[index - 2].equals(digit)))
{rngNumString[index] = digit;

现在,第二个检查点位于以下代码中:

Collections.addAll(randomSet,Arrays.toString(rngNumString)); //if not added individually, then a Hash code is passed and not the value.
randomSetTest = new HashSet<>(randomSet);}
randomList = new ArrayList<>(randomSetTest);

如您所知,集合不会返回重复的值。所以这应该确保我的系列不会重复。

然而,当我打印结果时,我得到的是:

This is a randomList: [[1, 1, 2], [1, 1, 3], [1, 1, 4], [1, 2, 3], [1, 2, 4], [1, 3, 2], [1, 3, 4], [1, 4, 2], [1, 4, 3], [2, 1, 3], [2, 1, 4], [2, 2, 1], [2, 2, 3], [2, 2, 4], [2, 3, 1], [2, 3, 4], [2, 4, 1], [2, 4, 3], [3, 1, 2], [3, 1, 4], [3, 2, 1], [3, 2, 4], [3, 3, 1], [3, 3, 2], [3, 3, 4], [3, 4, 1], [3, 4, 2], [4, 1, 2], [4, 1, 3], [4, 2, 1], [4, 2, 3], [4, 3, 1], [4, 3, 2], [4, 4, 1], [4, 4, 2], [4, 4, 3]]

This is the size of randomList: 36

您可以立即找出每个系列中的重复数字。现在我不问,也不想要挑战问题的答案。我只需要知道为什么我的检查点失败,请进行代码分析。

正如您所看到的,if 语句和集合的使用并没有阻止我的系列中的重复数字。我需要产生许多不重复的数字系列。因此,请不要为我解决挑战问题,而只是解决我的代码中的问题。我希望这一点是清楚的。我非常感谢您提供的任何帮助。

这是我的完整代码供您审阅:

void uniqueString() { //here begins uniqueString().


    Set<String> randomSet = new HashSet<>();
    Set<String> randomSetTest = new HashSet<>();

    String[] rngNumString = {"0","0","0"};
    ArrayList<String> randomList = new ArrayList<>();

    while (randomList.size() < 36) {

        for (int index = 0; index < rngNumString.length; index++) {
            Random rng = new Random();
            String digit = Integer.toString(rng.nextInt(4) + 1);

                if (index == 0 )
                    rngNumString[index] = digit;
                else if (index == 1 && (!rngNumString[index].equals(digit) && (!rngNumString[index - 1].equals(digit))))
                    rngNumString[index] = digit;
                else if (index == 2 && (!rngNumString[index].equals(digit)) && (!rngNumString[index - 1].equals(digit)) && (!rngNumString[index - 2].equals(digit)))
                {rngNumString[index] = digit;
                    Collections.addAll(randomSet,Arrays.toString(rngNumString)); //if not added individually, then a Hash code is passed and not the value.
                    randomSetTest = new HashSet<>(randomSet);}
                    randomList = new ArrayList<>(randomSetTest);

        }

    }

    Collections.sort(randomList);
    System.out.println("This is a randomList: " + randomList);
    System.out.println("This is the size of randomList: " + randomList.size());

}  //here ends uniqueString()
java list if-statement collections set
2个回答
1
投票

想象你最后得到的数字是 1,2,3。

现在发生以下情况:

  • 第一个随机数字 2,现在我们有 2,2,3,因为我们总是设置第一个。
  • 第二个随机数字2,该数字将被忽略,我们仍然有2,2,3。
  • 第三个随机数字 1,与现有数字不同,因此它被设置为
    rngNumString
    并且我们将 2,2,1 存储到集合中。

0
投票

你输出过多的原因是单个结果中出现了重复的数字,例如1,1,2,其中1出现了两次,它应该只出现一次。

我尊重你写的代码,所以我在你的基础上进行了修改

 static void uniqueString() { //here begins uniqueString().


    Set<String> randomSet = new HashSet<>();
    Set<String> randomSetTest = new HashSet<>();
    // No need for 0, so start from 1 
    String[] rngNumString = {"1", "1", "1"};
    ArrayList<String> randomList = new ArrayList<>();

    while (randomList.size() < 24) {

        for (int index = 0; index < rngNumString.length; index++) {
            Random rng = new Random();
            String digit = Integer.toString(rng.nextInt(4) + 1);
            if (index == 0)
                rngNumString[index] = digit;
            else if (index == 1 && (!rngNumString[index].equals(digit) && (!rngNumString[index - 1].equals(digit))))
                rngNumString[index] = digit;
            else if (index == 2 && (!rngNumString[index].equals(digit)) && (!rngNumString[index - 1].equals(digit)) && (!rngNumString[index - 2].equals(digit))) {
                rngNumString[index] = digit;
            }
            Set<String> set = new HashSet<>(Arrays.asList(rngNumString));
            if (set.size()<3) {continue;}
            Collections.addAll(randomSet, Arrays.toString(rngNumString)); //if not added individually, then a Hash code is passed and not the value.
            randomList = new ArrayList<>(randomSet);

        }

    }

    Collections.sort(randomList);
    System.out.println("This is a randomList: " + randomList);
    System.out.println("This is the size of randomList: " + randomList.size());

}

当你了解了更多内容,比如递归之后,你可以尝试理解这段代码。

public class Solution2 {
static List<List<Integer>> ans = new ArrayList<>();
public static void main(String[] args) {
    int[] nums = new int[]{1,2,3,4};
    boolean[] used = new boolean[nums.length];
    dfs(nums,0,used,new ArrayList<>());
    System.out.println(ans);
    System.out.println(ans.size());
}
public static void dfs(int[] nums, int index,boolean[] used,List<Integer> path) {
    if (path.size()==3) {
        ans.add(new ArrayList<>(path));
        return;
    }
    for (int i = 0; i < nums.length; i++) {
        if (used[i]) {
            continue;
        }
        used[i] = true;
        path.add(nums[i]);
        dfs(nums, index+1, used, path);
        used[i] = false;
        path.remove(path.size()-1);
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.