if-else语句中的多个布尔值在Java中始终为false



由于if-else条件表明

  • Condition usersInSales && usersInPayments is always false
  • Condition usersInSales && usersInLoans is always false
  • Condition usersInPayments && usersInLoans is always false

我尝试了不同的条件组合,并添加了错误值来尝试解决它,但没有帮助。请帮我一下好吗?提前感谢

private List<UserResource> usersFilteredByDepartment(List<UserResource> users, boolean usersInSales, boolean usersInPayments, boolean usersInLoans) {
if (usersInSales) {
return getUsersInSales(users);
} else if (usersInPayments) {
return getUsersInPayments(users);
} else if (usersInLoans) {
return getUsersInLoans(users);
} else if (usersInSales && usersInPayments) {
return Stream.concat(getUsersInSales(users).stream(), getUsersInPayments(users).stream()).distinct().collect(Collectors.toList());
} else if (usersInSales && usersInLoans) {
return Stream.concat(getUsersInSales(users).stream(), getUsersInLoans(users).stream()).distinct().collect(Collectors.toList());
} else if (usersInPayments && usersInLoans) {
return Stream.concat(getUsersInPayments(users).stream(), getUsersInLoans(users).stream()).distinct().collect(Collectors.toList());
} else return users;
}

您有两个解决方案。

  1. 如其他人所示,重新排序您的条件。在您的代码中,当您点击&&语句时,您已经处理了一半为真的情况。&&(两个子句(比单个子句更具限制性。

  2. 或者,将double子句放在前面的if中

    if (usersInSales) {
    return getUsersInSales(users);
    } else if (usersInPayments) {
    return getUsersInPayments(users);
    } else if (usersInLoans) {
    return getUsersInLoans(users);
    } else if (usersInSales && usersInPayments) {
    return Stream.concat(getUsersInSales(users).stream(), getUsersInPayments(users).stream()).distinct().collect(Collectors.toList());
    } else if (usersInSales && usersInLoans) {...
    

变为

if (usersInSales) {
if (usersInPayments) { // Nested if is like && operator.
return Stream.concat(getUsersInSales(users).stream(), getUsersInPayments(users).stream()).distinct().collect(Collectors.toList());
} else {
return getUsersInSales(users);
}
} else if (usersInPayments) { ...

这是一个更有效的一点,我认为一般来说更可取。

if (usersInSales) {
return getUsersInSales(users);
} else if (usersInPayments) {    ---> You get here when userInSales = false
return getUsersInPayments(users);
} else if (usersInLoans) {  --> You get here when usersInSales = false && usersInPayments = false
return getUsersInLoans(users);
}  else if () {  --> You get here when usersInSales = false && usersInPayments = false && usersInLoans = false. No use in comparing what you compare here. It will be always false as it reports to
}

希望你现在能想出办法摆脱

您可以尝试先检查最具体的约束,然后再转到更通用的约束。

if (usersInSales && usersInPayments) {
return Stream.concat(getUsersInSales(users).stream(), getUsersInPayments(users).stream()).distinct().collect(Collectors.toList());
} else if (usersInSales && usersInLoans) {
return Stream.concat(getUsersInSales(users).stream(), getUsersInLoans(users).stream()).distinct().collect(Collectors.toList());
} else if (usersInPayments && usersInLoans) {
return Stream.concat(getUsersInPayments(users).stream(), getUsersInLoans(users).stream()).distinct().collect(Collectors.toList());
} else if (usersInSales) {
return getUsersInSales(users);
} else if (usersInPayments) {
return getUsersInPayments(users);
} else if (usersInLoans) {
return getUsersInLoans(users);
} else return users;

检查你的工作流程,然而,如果没有所有这些if,else检查,IMO你的方法会更有意义这就足够了

private List<UserResource> usersFilteredByDepartment(List<UserResource> users, boolean usersInSales, boolean usersInPayments, boolean usersInLoans) {

return Stream.concat(
usersInPayments? getUsersInPayments(users).stream(): Stream.empty(),
usersInLoans? getUsersInLoans(users).stream(): Stream.empty(), 
usersInSales? getUsersInSales(users).stream(): Stream.empty()
).distinct().collect(Collectors.toList());

}

考虑一下您的逻辑:首先检查usersInSales,如果它为true,则返回。然后检查usersInPayments,如果它是真的,则返回。因此,当您检查usersInSales && usersInPayments时,两者都将为false,因为您已经处理了其中任何一个为true的情况。

您需要更改您的逻辑,以便处理附加条件而不是独占条件。

您的最后3个条件将永远不会运行。因为你的前三个条件中的一个已经成立了。要解决这个问题,你需要颠倒你的条件顺序,试试这个:

if (usersInSales && usersInPayments) {
} else if (usersInSales && usersInLoans) {
} else if (usersInPayments && usersInLoans) {
} else if (usersInSales ) {
} else if ( usersInLoans) {
} else if (usersInLoans) {
} else return users;

相关内容

  • 没有找到相关文章

最新更新