[英]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.