【问题标题】:how can I come out of if block?我怎样才能摆脱 if 块?
【发布时间】:2012-12-26 02:00:16
【问题描述】:

考虑一下这个代码示例。

这段代码只是用来解释我的问题。

boolean status = false;
for ( int i = 0; i <= 5; i++ ) {
    if ( i == 4 ) { 
        System.out.println ( "Enter into first if" );
        if ( status == false ) {
               System.out.println ( "Enter into second if" );
               status = true;
               if ( status == true ) {
                    System.out.println ( "Enter into third if" );
                    //third if body
               }
               //second if body                             
        }
        //first if body
        System.out.println ( "Before exiting first if" );
    }
}

我想做的就是从第三个如果到第一个如果。

我们知道 break, continue 可以在 loops 中使用。我们可以为 blocks 实现同样的效果吗?

【问题讨论】:

  • 为什么不直接更改if 语句?
  • 目前还不清楚您要达到什么目的。在这种情况下,不要询问如何转义 if,而是查看实际问题并重构代码以满足您的需求。
  • 改用switch 语句?
  • 如果你不想超过一个,最坏的情况下使用标志变量... ;)
  • 为什么有一行 status 被设置为 true,然后在检查 status 是否为 true 之后?

标签: java if-statement controls


【解决方案1】:

添加while 循环和break 语句的简单组合可能会有所帮助。

boolean status = false;
for ( int i = 0; i <= 5; i++ ) {
    if ( i == 4 ) { 
        System.out.println ( "Enter into first if" );
        while(True) {
            if ( status == false ) {
                System.out.println ( "Enter into second if" );
                status = true;
                if ( status == true ) {
                    System.out.println ( "Enter into third if" );
                    break;//The key statement
                    //third if body
                }
                //second if body                             
            }
            break;//Don't forget break while loop
        }          
        //first if body
        System.out.println ( "Before exiting first if" );
    }
}

或者你可以使用try .. catch ..

【讨论】:

    【解决方案2】:

    我会引入第二个状态标志来捕捉最里面的条件:

    boolean secondStatus = false;
    boolean status = false;
    for ( int i = 0; i <= 5; i++ ) {
        if ( i == 4 ) { 
            System.out.println ( "Enter into first if" );
            if ( status == false && secondStatus == false ) {
                   System.out.println ( "Enter into second if" );
                   status = true;
                   if ( status == true ) {
                        System.out.println ( "Enter into third if" );
                        //third if body
                        secondStatus = true;
                   } else {
                       //second if body
                   }                           
            }
            //first if body
            System.out.println ( "Before exiting first if" );
        }
    }
    

    【讨论】:

    • 不完全是,这并没有超出第三个 if。它只是忽略下一个循环中的第二个 if。如果“//second if body”不是注释而是某行代码,则此方法不起作用。
    【解决方案3】:

    创建一个封装if#2和#3的方法。然后从#3 if 块内部return

    boolean status = false;
    for ( int i = 0; i <= 5; i++ ) {
        if ( i == 4 ) { 
            System.out.println ( "Enter into first if" );
            status = perform2ndAnd3rdIf(status);
            System.out.println ( "Before exiting first if" );
        }
    }
    
    private boolean perform2ndAnd3rdIf(boolean status) {
        if ( status == false ) {
            System.out.println ( "Enter into second if" );
            status = true;
            if ( status == true ) {
                System.out.println ( "Enter into third if" );
                return status;
                //third if body
            }
            //second if body                             
        }
        return status;
    }
    

    使用方法而不是嵌套的if 块可以提高整体可读性和理解代码的能力。而不是代码 cmets,您应该给该方法一个有意义的名称(当然不是 perform2ndAnd3rdIf())。

    【讨论】:

      【解决方案4】:

      嗯,你可以打破任何块:

      public class Bar {
      
        static void foo(boolean b) {
          foo: {
            if (b) {
              break foo;
            }
            System.out.println(b);
          }
        }
      
        public static void main(String[] args) {
          foo(true);
          foo(false);
        }
      }
      

      输出:

      false
      

      请参阅Java Language Specification 了解更多信息。

      但是,如果您尝试在我的一个项目中提供此代码,我们可能会说出来。

      【讨论】:

        【解决方案5】:

        你总是可以这样做:

        mylabel:
        boolean status = false;
        for ( int i = 0; i <= 5; i++ ) {
            if ( i == 4 ) { 
                System.out.println ( "Enter into first if" );
                if ( status == false ) {
                       System.out.println ( "Enter into second if" );
                       status = true;
                       if ( status == true ) {
                            System.out.println ( "Enter into third if" );
        
                            break mylabel;
        
                            //third if body
                       }
                       //second if body                             
                }
                //first if body
                System.out.println ( "Before exiting first if" );
            }
        }
        

        但我认为你应该重构你的代码。

        【讨论】:

        • 我认为 OP 希望第一个 if 语句继续执行 - “我要做的就是从第三个 if 到第一个 if。” - 这意味着您将 mylabel: 放在第二个 if 之前(例如“mylabel: if (status == false)”),因此会在第二个 if 结束时中断,但我认为我们需要OP澄清!
        • 我随意放置标签只是为了说明如何做,但感谢您指出。
        【解决方案6】:

        我不确定你试图通过这个实现的要求。在第三个循环之后再添加一个 if 条件,如果仅满足条件,则执行第二个循环的剩余部分。你可以在里面设置这个条件第三个 if 块。

        您可以直接切换到“switch”,这样会更干净。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-01-27
          • 2020-08-10
          • 1970-01-01
          • 1970-01-01
          • 2023-03-08
          • 2022-12-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多