简体   繁体   English

java有条件的我如何确保它检查所有的if语句

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

相关问题 Java 8如何确保我们的代码检查完整的证书链? - Java 8 How to make sure our code checks the full certificate chain? 如何确保java程序与Java Enviornment 6兼容? - How do I make sure that a java program is compatible with Java Enviornment 6? 如何确保在 Java 中只选择了一个 JRadioButton? - How Do I Make Sure Only One JRadioButton Is Selected In Java? 如何在java中检查条件语句的大括号的逻辑? - how to make logic for checking braces for conditional statements in java? 如何确定按钮已选中? - How do I make sure that the button is checked? 如何确保我将所有小数点后的所有数字从Excel导出到MySQL - How do I make sure I get all the digits past the decimal out of Excel and into MySQL 对于 Java,如果我想做一个 if 语句以确保输入只包含一个字符,我应该如何进行? - For Java, if I want to do an if statement to make sure the input only takes in one character, how should I proceed? 如何确保我在项目中使用最新的Java库? - How do I make sure I am using the latest java libraries in my projects? 在Java中无需使用条件语句就可以按升序排列3个数字。 正如我完全不能使用if语句 - 3 numbers in ascending order WITHOUT the use of conditional statements in Java. As in I can't use if statements at all 在JBoss应用程序服务器中,如何确保部署中的所有模块对jar库使用相同的Classloader? - In JBoss application server, how do I make sure that all modules in a deployment use the same Classloader for a jar library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM