if else 语句中的多个布尔值在 Java 中始终为 false
Multiple boolean values in if else statement is always false in Java
我坚持使用这个方法,因为 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;
}
考虑一下您的逻辑:您首先检查 usersInSales
,然后检查 return 是否为真。然后检查 usersInPayments
和 return 是否为真。因此,当您随后检查 usersInSales && 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 检查,您的方法会更有意义。 这个就够了
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());
}
你的最后 3 个条件永远不会 运行。因为您的前 3 个条件之一已经为真。要解决此问题,您需要颠倒条件的顺序,试试这个:
if (usersInSales && usersInPayments) {
} else if (usersInSales && usersInLoans) {
} else if (usersInPayments && usersInLoans) {
} else if (usersInSales ) {
} else if ( usersInLoans) {
} else if (usersInLoans) {
} else return users;
您有 两个 解。
重新排序您的条件,如其他人所示。在您的代码中,当您点击 &&
语句时,您已经处理了一半为真的情况。 &&
(两个子句)比单个子句更具限制性。
或者,将双子句放在前面的 ifs 中。
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 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;
}
考虑一下您的逻辑:您首先检查 usersInSales
,然后检查 return 是否为真。然后检查 usersInPayments
和 return 是否为真。因此,当您随后检查 usersInSales && 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 检查,您的方法会更有意义。 这个就够了
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());
}
你的最后 3 个条件永远不会 运行。因为您的前 3 个条件之一已经为真。要解决此问题,您需要颠倒条件的顺序,试试这个:
if (usersInSales && usersInPayments) {
} else if (usersInSales && usersInLoans) {
} else if (usersInPayments && usersInLoans) {
} else if (usersInSales ) {
} else if ( usersInLoans) {
} else if (usersInLoans) {
} else return users;
您有 两个 解。
重新排序您的条件,如其他人所示。在您的代码中,当您点击
&&
语句时,您已经处理了一半为真的情况。&&
(两个子句)比单个子句更具限制性。或者,将双子句放在前面的 ifs 中。
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) { ...
这样效率更高一些,我认为通常更可取。