繁体   English   中英

Java中的if条件如何进入内部

[英]How to break inside if condition in java

我有以下方法。 我正在其中调用一个布尔方法。如果该布尔方法为true,则从块中退出,否则继续执行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);
    }
}

您可以使用;

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

但我真的认为您应该质疑自己要完成的工作。

我需要完全退出method1()。 我不想继续其他部分。如何在此处中断/退出...并继续进行应用程序。 如果为假,则执行其他部分

这是if else阻止的目标。

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

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

如果在此特定情况下不想忽略的if语句之后还有其他处理, if可以使用return语句。 但在这种情况下,你不需要夫妇if你想要做一个return与其他else if块,因为它们不依赖于:

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

if(...){
   ...
}
else{
   ...
}
// do some processing

无需在if主体中放置任何内容,如果iftrue ,则跳过else 但是,使用布尔值否定会更干净,例如

public void method1(){
    A a = new A();
    if (!a.method2()) {
        method3(); //<-- block not entered if method2() returns true.
    }
}
public void method1(){
    A a = new A();
    if(a.method2() == false){
        continue with method3();
    }
}

暂无
暂无

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

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