2012-06-27 136 views
0

在我的IF語句中,它有點奇怪,導致每次我的代碼循環時,只有SLOT1被調用並顯示在我的調試器中,爲什麼?我的發言有問題嗎?謝謝。IF語句比較

choiceSlot1 = TRUE; 
    choiceSlot2 = TRUE; 
    choiceSlot3 = TRUE; 
    choiceSlot4 = TRUE; 

    if (slot1 != [(UIImageView*)[self.carousel1 currentItemView] tag] || twoSlot1 != [(UIImageView*)[self.carousel2 currentItemView] tag] || choiceSlot1) { 

     NSLog(@"Slot1"); 
     choiceSlot1 = FALSE; 

    } 
    else if (slot2 != [(UIImageView*)[self.carousel1 currentItemView] tag] || twoSlot2 != [(UIImageView*)[self.carousel2 currentItemView] tag] || choiceSlot2) { 

     NSLog(@"Slot2"); 
     choiceSlot2 = FALSE; 


    } 
    else if (slot3 != [carousel1 indexOfItemViewOrSubview:carousel1.superview] || twoSlot3 != [(UIImageView*)[self.carousel2 currentItemView] tag] || choiceSlot3) { 

     NSLog(@"Slot3"); 
     choiceSlot3 = FALSE; 


    } 
    else if (slot4 != [(UIImageView*)[self.carousel1 currentItemView] tag] || twoSlot4 != [(UIImageView*)[self.carousel2 currentItemView] tag] || choiceSlot4) { 

     NSLog(@"Slot4"); 
     choiceSlot4 = FALSE; 

    } 
+2

那麼,你將choiceSlot1設置爲true,所以它總是進入第一個if。 – borrrden

+0

'choiceSlot1'始終爲TRUE .. – Kjuly

+0

我應該如何處理if語句的其餘部分? – Bazinga

回答

1

因爲else if在情況下,只有驗證的首if語句是false

例子:

if (true) 
{ 
    // Will be executed 
} 
else if (true) 
{ 
    // Will not be executed 
} 

if (false) 
{ 
    // Will not be executed 
} 
else if (true) 
{ 
    // Will be executed 
} 
+0

我應該如何處理if語句的其餘部分? – Bazinga

+1

在if之前刪除'else'。換句話說,做四個單獨的'if'語句。 – Anne

+0

是的,然後在所有它被記錄。 – Bazinga

1

這很容易,

if (slot1 != [(UIImageView*)[self.carousel1 currentItemView] tag] || twoSlot1 != [(UIImageView*)[self.carousel2 currentItemView] tag] || choiceSlot1) 

如果choiceSlot1爲TRUE,則第一個語句總是要真(用OR,其真正如果要素之一是真的)。

當第一條語句爲true時,其餘的else/if不會被調用!