2013-01-11 63 views
2

考慮這個代碼示例。我怎麼能出來如果塊?

此代碼只是爲了解釋我的問題。

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"); 
    } 
} 

所有我想要做的是從第三齣來,如果首先如果。

我們知道休息,繼續是可以在使用那裏。我們可以達到相同的

+0

爲什麼不只是改變if語句呢? –

+2

重構你的代碼。 – Schaliasos

+3

現在還不清楚你試圖達到什麼目標。在這種情況下,不要問如何逃避if,查看實際問題並重構代碼以滿足您的需求。 –

回答

3

好了,你可以打破任何塊:

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

但是,如果您嘗試在我的一個項目中提供此代碼,我們可能會有單詞。

1

我不確定你的要求,你試圖通過this.Put多一個,如果條件後第三個循環,如果條件只滿足,執行第二個循環的剩餘部分。您可以設置此條件裏面第三如果塊。

而不是所有這些,你可以切換到'開關',這將是更清潔。

3

創建封裝#2和#3的方法。從#3的內部,如果塊然後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塊的整體提高可讀性和理解你的代碼的能力。代替註釋,您應該給該方法一個有意義的名稱(當然而不是perform2ndAnd3rdIf())。

1

我介紹第二個狀態標記趕上那最裏面的條件:

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"); 
    } 
} 
+0

這個問題中解釋的不太完美,如果不是這樣的話。它只是在下一個循環中忽略第二個。如果「// second if body」不是註釋,而是一些代碼行,這不起作用。 –

+0

好點。更新。 –

1

你總是可以做到這一點:

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"); 
    } 
} 

,但我認爲你應該重構你的代碼來代替。

+0

我想OP希望第一個if語句繼續執行 - 「如果第一個if,我想要做的就是從第三個出來。」 - 這意味着你在第二個if(例如「mylabel:if(status == false)」)之前立即放置* mylabel:*,因此如果出現了第二個的結尾,但我認爲我們需要OP來澄清! – xagyg

+0

我隨意放置了標籤,以表明它如何完成,但感謝您指出它。 –

0

添加while循環和break語句的簡單組合可能有所幫助。或者您可以使用try .. catch ..

相關問題