简体   繁体   English

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

[英]Multiple boolean values in if else statement is always false in Java

I am stuck to this method because of the if else condition says that我坚持这种方法,因为 if else 条件说

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

I tried different condition combinations and added the false values to try resolve it but it didn't help.我尝试了不同的条件组合并添加了错误值来尝试解决它,但没有帮助。 Please can I have some help?请问我有什么帮助吗? Thanks in advance提前致谢

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;
    }
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
     }

Hope you now can figure your way out希望你现在能找到出路

You could try to check first the most specific constraints and then in the end move to more general constraints.您可以尝试首先检查最具体的约束,然后最后转向更一般的约束。

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;

Checking your workflow however IMO your method would have more sense without all those if, else checks.检查您的工作流程,但是 IMO,如果没有所有这些 if、else 检查,您的方法会更有意义。 Just this could be enough仅此就足够了

 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());
    
}

You have two solutions.你有两个解决方案。

  1. Reorder your conditions, as others have shown.正如其他人所示,重新排列您的条件。 In your code by time you hit the && statements you have already dealt with the cases when one half is true.在您的代码中,当您点击&&语句时,您已经处理了一半为真的情况。 The && (two clauses) is more restrictive than a single clause. && (两个子句)比单个子句更具限制性。

  2. Alternatively, put the double clauses inside the previous ifs.或者,将双子句放在前面的 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) {...

becomes变成

    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) { ...

This is a little more efficient and I think generally preferable.这效率更高一些,我认为通常更可取。

Consider your logic: you first check usersInSales , and return if it's true.考虑您的逻辑:您首先检查usersInSales ,如果为真则返回。 Then you check usersInPayments and return if it's true.然后您检查usersInPayments并返回是否为真。 So when you then check usersInSales && usersInPayments , both will be false because you already handled the cases when either of them are true.因此,当您检查usersInSales && usersInPayments ,两者都将为 false,因为您已经处理了其中一个为 true 的情况。

You need to change your logic so you handle the conditions additive instead of exclusively.您需要更改您的逻辑,以便您可以添加而不是专门处理条件。

your last 3 conditions will never run.您的最后 3 个条件将永远不会运行。 because one of your first 3 conditions will already be true.因为您的前三个条件之一已经成立。 To fix this you need to reverse the order of your conditions, try this:要解决此问题,您需要颠倒条件的顺序,请尝试以下操作:

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

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

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