2016-12-04 35 views
-1

假設:的條件3 1將是0檢查以查看If語句中的三個條件中的哪一個是否爲真否則如果其中一個不是?

如果我有一個,如果定義聲明如下:

if ((condition1 == 0) || condition2 == 0) || condition3 == 0)) { 
    do something 
    } 

和3 if語句下面的東西:

else if (condition1 != 0) { 
    do something 
    print which of condition 2 or 3 was 0 
} 

else if (condition2 != 0) { 
    do something 
    print which of condition 1 or 3 was 0 
} 

else if (condition3 != 0) { 
    do something 
    print which of condition 1 or 2 was 0 
} 

我想着也許在第一個if語句中嵌入if語句,以查看三者中的哪一個是0.

+0

有什麼問題? – Maroun

+0

歡迎來到Stack Overflow!請查看我們的[SO問題清單](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist)來幫助你提出一個好問題,從而得到一個很好的答案。 –

+0

是的,如果你想知道哪種情況是真的。 – SMA

回答

0

想出了一些東西。儘管如此,許多if語句使用了多麼可怕的做法。 (假設memorizeCondition存儲IFS的方法外)

if (condition1=0 || condition2 = 0 || condition3 = 0) 
    if(condition1 = 0) 
     memorizeCondition = condition1; 
    if(condition2 = 0) 
     memorizeCondition = condition2; 
    if(condition3 = 0) 
     memorizeCondition = condition3; 
else if (condition1 != 0) 
    memorizeCondition; 
    calculate condition1; 
    calculate other condition; 
else if (condition2 != 0) 
    memorizeCondition; 
    calculate condition2; 
    calculate other condition; 
else if (condition3 != 0) 
    memorizeCondition; 
    calculate condition2; 
    calculate other condition; 
0

你看這個:

if ((condition1 == 0) || condition2 == 0) || condition3 == 0)) { 
     do something 
    } else { 
     if (condition1 != 0) { 
     do something 
     print which of condition 2 or 3 was 0 
     } 
     if (condition2 != 0) { 
     do something 
     print which of condition 1 or 3 was 0 
     } 
     if (condition3 != 0) { 
     do something 
     print which of condition 1 or 2 was 0 
     } 
    } 

如果有很多超過3個條件,你可以將它們添加到集合,然後使用流。 filter()刪除所有不匹配的條件。

相關問題