[英]java conditional how do I make sure it checks all if statements
I am having trouble getting the below query method to work. 我无法让以下查询方法正常工作。 I need it to return if
isRestricted
all of the reports with restricted in the status. 如果
isRestricted
所有状态为受限的报告,我都需要它返回。 If isClosed then return all reports that are closed for the status. 如果为isClosed,则返回所有已关闭状态的报告。 It works for
isRestricted
, returns all reports in restricted status. 它适用于
isRestricted
,以受限状态返回所有报告。 It also works for returning everything except restricted and closed which is good, but just returns everything for the if isClosed
condition. 它也可以返回除约束和关闭之外的所有内容,这很好,但是只返回if
isClosed
条件的所有内容。 Now if I move the isClosed
conditions to the top then it works for isClosed, but doesn't work for isRestricted
. 现在,如果将
isClosed
条件移到顶部,则它适用于isClosed,但不适用于isRestricted
。 What am I missing? 我想念什么?
protected String getRestrictedOrClosedTerm() {
if (isRestricted == null || "false".equals(isRestricted)) {
return "-status:restricted -status:closed ";
}
if (isRestricted.isEmpty()) {
return "";
}
if ("true".equals(isRestricted)) {
return "+status:restricted ";
}
if (isClosed == null || "false".equals(isClosed)) {
return "-status:closed ";
}
if (isClosed.isEmpty()) {
return "";
}
if ("true".equals(isClosed)) {
return "+status:closed ";
}
return "";
}
It is like it is stopping at the: 就像它停止在:
if ("true".equals(isRestricted)) {
return "+status:restricted ";
}
Interestingly if I use this I get the desired results, but not sure why: 有趣的是,如果使用此选项,我会得到理想的结果,但不确定为什么:
protected String getRestrictedOrClosedTerm() {
if ("true".equals(isClosed)) {
return "+status:closed ";
}
if ("true".equals(isRestricted)) {
return "+status:restricted ";
}
if (isRestricted == null || "false".equals(isRestricted)) {
return "-status:restricted -status:closed ";
}
return "";
}
When you use a return
in a method, it exits that method with the value returned. 当您在方法中使用
return
时,它将以返回的值退出该方法。 If you use something like a StringBuilder , you can see all the output: 如果使用StringBuilder之类的东西,则可以看到所有输出:
protected String getRestrictedOrClosedTerm()
{
StringBuilder sb = new StringBuilder();
if (isRestricted == null || "false".equals(isRestricted)) {
sb.append("-status:restricted -status:closed\n");
}
if (isRestricted.isEmpty()) {
sb.append("empty\n");
}
if ("true".equals(isRestricted)) {
sb.append("+status:restricted\n");
}
if (isClosed == null || "false".equals(isClosed)) {
sb.append("-status:closed\n");
}
if (isClosed.isEmpty()) {
sb.append("empty\n");
}
if ("true".equals(isClosed)) {
sb.append("+status:closed\n");
}
return sb.toString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.