[英]If statement executed even if the condition is false
我有一种生成随机数的方法,但我希望其中一些被丢弃。 这是代码:
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;
}
即使System.out.println(.Arrays.asList(toExclude);contains(random));
如果执行,则打印 false 并且我得到一个错误的随机数
while 循环中有不正确的逻辑。 只要有一个数字需要排除,您就需要执行循环,而不是相反。
只需您的代码即可:
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;
}
我宁愿使用集合而不是列表。 优点是您可以将元素添加到集合中,具体取决于元素是否已存在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;
}
但小心点! 如果 exclude 包含 0 和 max-1 之间的所有数字,则此方法和@Nicholas K
的方法都以无限循环结束。 为防止出现这种情况,请添加对 arguments 的验证,如下所示。 与列表相比,集合还有另一个优点,因为您不必先过滤重复项。
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.