我不確定如何在標題中具體說明這個問題,我知道這可能是一個非常愚蠢的問題,但仍然......:D
有沒有更好的方法來檢查這種情況:
if (bool1 && !bool2) {
#different code1
#samecode
}
else if (!bool1 && bool2)
{
#different code2
#samecode
}
在此示例中,應檢查代碼的某些部分以滿足這些條件,但某些部分應該起作用。矛盾的陳述句不允許我在一個條件下合并它們。有沒有其他方法可以寫下這種情況,這樣我就不必復制/粘貼代碼了?
uj5u.com熱心網友回復:
如果您的目標是避免重復#samecode,您可以使用Exclusive OR (AKA, XOR) 運算子,如下所示:
if (bool1 ^ bool2) // <-- If bool1 is true or bool2 is true but not both.
{
if (bool1)
{
// #different code1
}
else
{
// #different code2
}
// #samecode
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/419873.html
標籤:
上一篇:VisualStudio2019#cRedirectNotWorkingonprocess.start檔案找不到
下一篇:C#試圖將二進制數填充到4位塊
