简体   繁体   English

即使条件为假也执行 if 语句

[英]If statement executed even if the condition is false

I have a method that generate random number but i want some of them to be trashed.我有一种生成随机数的方法,但我希望其中一些被丢弃。 This is the code:这是代码:

    public static int getRandomX(int max, int[] exclude){
        Random randomGenerator = new Random();
        Integer[] toExclude = Arrays.stream( exclude ).boxed().toArray(Integer[]::new);
        Integer random = Integer.valueOf(randomGenerator.nextInt(max));
        do{
            System.out.println(!Arrays.asList(toExclude).contains(random));
            if(!(Arrays.asList(toExclude).contains(random))){
                return random;
            } else{
                random ++;
            }
        }while(!Arrays.asList(toExclude).contains(random));
        return random;
    }   

Even if the System.out.println(.Arrays.asList(toExclude);contains(random));即使System.out.println(.Arrays.asList(toExclude);contains(random)); print false the if get executed and i get a wrong random number如果执行,则打印 false 并且我得到一个错误的随机数

There is an incorrect logic in the while loop. while 循环中有不正确的逻辑。 You need to execute the loop as long as there is a number that needs to be excluded and not the other way around.只要有一个数字需要排除,您就需要执行循环,而不是相反。

Simply your code to:只需您的代码即可:

public static int getRandomX(int max, int[] exclude) {
    Random randomGenerator = new Random();
    Integer[] toExclude = Arrays.stream(exclude).boxed().toArray(Integer[]::new);
    Integer random;
    do {
        random = Integer.valueOf(randomGenerator.nextInt(max));
    } while (Arrays.asList(toExclude).contains(random));
    return random;
}

I'd rather work with sets than lists.我宁愿使用集合而不是列表。 The advantage is you can add an element to a set and depending on whether the element already exists or not set.add returns true or false, making your while loop easier and more readable:优点是您可以将元素添加到集合中,具体取决于元素是否已存在set.add返回 true 或 false,从而使您的 while 循环更容易且更具可读性:

public static int getRandomX(int max, int[] exclude){
    Set<Integer> set = Arrays.stream(exclude).boxed().collect(Collectors.toSet());
    Random randomGenerator = new Random(max);
    Integer random = randomGenerator.nextInt(max);
    while(!set.add(random)){
        random = randomGenerator.nextInt(max);
    }
    return random;
}

But be careful!但小心点! Both this approach and that of @Nicholas K ends in an endless loop if exclude contains all numbers between 0 and max-1.如果 exclude 包含 0 和 max-1 之间的所有数字,则此方法和@Nicholas K的方法都以无限循环结束。 To prevent this, add a validation of the arguments as given below.为防止出现这种情况,请添加对 arguments 的验证,如下所示。 There is also another advantage of sets over lists since you don't have to filter duplicates first.与列表相比,集合还有另一个优点,因为您不必先过滤重复项。

public static int getRandomX(int max, int[] exclude){
    Set<Integer> set = Arrays.stream(exclude).boxed().collect(Collectors.toSet());
    if(set.size() == max){
        throw new IllegalArgumentException("All numbers in range [0 - " +(max-1)+"] excluded");
        //or return -1;
    }
    Random randomGenerator = new Random(max);
    Integer random = randomGenerator.nextInt(max);
    while(!set.add(random)){
        random = randomGenerator.nextInt(max);
    }
    return random;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM