如果java中的条件如何打破内部

时间:2017-01-11 10:59:05

标签: java

我有以下方法。我在其中调用一个布尔方法。如果布尔方法为真,则从块中退出,否则继续使用else块。以下是我的尝试。但未能打破方法。

public IStatus validate() throws ExecutionValidationException {
    ConfigurationHandler handler = new ConfigurationHandler(getExecutionParameters());
    if (handler.isModelValidationEnabled()){
      //how to handle here and exit here. i need to continue the application
    } else
        this.loggerStorage.getLogger().info(VALIDATION_DM_START);
    try {
        return getDataModel().validateModel();
    } finally {
        this.loggerStorage.getLogger().info(VALIDATION_DM_FINISHED);
    }
}

4 个答案:

答案 0 :(得分:0)

你可以使用;

if(a.method2() == true){
    return;
}

但我真的认为你应该质疑你想要完成的事情。

答案 1 :(得分:0)

  

我需要完全退出method1()。我不想继续其他   part.How我可以在这里打破/退出...并继续申请。   如果为false,则执行else part

这是if else阻止的目标。

如果if语句为true,则永远不会到达else块。

if(a.method2() == true){
   ...
}
else{
   ...
}

如果您在return语句之后进行了一些其他处理,您可以使用if语句,在这种特定情况下您不想忽略。 但在这种情况下,您不需要将要if的{​​{1}}与其他return块绑在一起,因为它们不依赖:

else if

答案 2 :(得分:0)

如果ifelse,则无需在if正文中放置任何内容,true会被跳过。但是,使用布尔否定(如

)会更清晰
public void method1(){
    A a = new A();
    if (!a.method2()) {
        method3(); //<-- block not entered if method2() returns true.
    }
}

答案 3 :(得分:0)

public void method1(){
    A a = new A();
    if(a.method2() == false){
        continue with method3();
    }
}