2016-08-12 43 views
-4

我試圖做的,如果(SOMETHING & & NOT)OR(SOMETHING & & NOT)...但它不工作,你可以看到我做了什麼錯?什麼是IF的語法和或C#

if (MyBeam.Position.Rotation == Position.RotationEnum.FRONT && 
!(MyBeam.Profile.ProfileString.Contains("RHS") 
|| MyBeam.Profile.ProfileString.Contains("CHS") 
|| MyBeam.Profile.ProfileString.Contains("MET") 
|| MyBeam.Profile.ProfileString.Contains("SHS") 

|| 

(MyBeam.Position.Rotation == Position.RotationEnum.BACK && 
!(MyBeam.Profile.ProfileString.Contains("RHS") 
|| MyBeam.Profile.ProfileString.Contains("CHS") 
|| MyBeam.Profile.ProfileString.Contains("MET") 
|| MyBeam.Profile.ProfileString.Contains("SHS"))))) 

忽略對API的引用,但我相信它仍然有意義。

+0

哪一部分無法爲你工作? – Biscuits

+0

結合起來嗎? if((MyBeam.Position.Rotation == Position.RotationEnum.FRONT || MyBeam.Position.Rotation == Position.RotationEnum.BACK)...' – Ric

+1

只需簡單的包含位,就可以使用數組'myArray.Any(MyBeam.Profile.ProfileString.Contains)',其中'myArray'是一個「RHS」,「CHS」的數組......' – Matthew

回答

2

你有你的括號錯誤。這一個應該工作:

if ((MyBeam.Position.Rotation == Position.RotationEnum.FRONT && 
    !(MyBeam.Profile.ProfileString.Contains("RHS") || 
     MyBeam.Profile.ProfileString.Contains("CHS") || 
     MyBeam.Profile.ProfileString.Contains("MET") || 
     MyBeam.Profile.ProfileString.Contains("SHS"))) || 
    (MyBeam.Position.Rotation == Position.RotationEnum.BACK && 
    !(MyBeam.Profile.ProfileString.Contains("RHS") || 
     MyBeam.Profile.ProfileString.Contains("CHS") || 
     MyBeam.Profile.ProfileString.Contains("MET") || 
     MyBeam.Profile.ProfileString.Contains("SHS")))) 

這就是說,這是非常不可讀的。嘗試結合一些人進入的變量,例如:

bool profileCheck = MyBeam.Profile.ProfileString.Contains("RHS") || MyBeam.Profile.ProfileString.Contains("CHS") || 
        MyBeam.Profile.ProfileString.Contains("MET") || MyBeam.Profile.ProfileString.Contains("SHS"); 

if ((MyBeam.Position.Rotation == Position.RotationEnum.FRONT && !profileCheck) || 
    (MyBeam.Position.Rotation == Position.RotationEnum.BACK && !profileCheck)) 

在這種形式下,我可以看到,它可以簡化爲:

if (!profileCheck && (MyBeam.Position.Rotation == Position.RotationEnum.FRONT || MyBeam.Position.Rotation == Position.RotationEnum.BACK)) 
+0

完美,謝謝! – gazeranco

0

你是說你想旋轉是正面還是背面,你不想RHS但接受CHS,MET,SHS?

if ((MyBeam.Position.Rotation == Position.RotationEnum.FRONT || MyBeam.Position.Rotation == Position.RotationEnum.BACK) && 
(
!MyBeam.Profile.ProfileString.Contains("RHS") 
|| MyBeam.Profile.ProfileString.Contains("CHS") 
|| MyBeam.Profile.ProfileString.Contains("MET") 
|| MyBeam.Profile.ProfileString.Contains("SHS") 
) 
) 

,或者如果你是說,你需要的旋轉方向是正面或背面,你不希望任何RHS,CHS,MET的,SHS?

if ((MyBeam.Position.Rotation == Position.RotationEnum.FRONT || MyBeam.Position.Rotation == Position.RotationEnum.BACK) && 
!(
MyBeam.Profile.ProfileString.Contains("RHS") 
|| MyBeam.Profile.ProfileString.Contains("CHS") 
|| MyBeam.Profile.ProfileString.Contains("MET") 
|| MyBeam.Profile.ProfileString.Contains("SHS") 
) 
) 
+0

否, 我在嘗試着說==前面或後面&&不是RHS,CHS,MET,SHS – gazeranco

0

您的不是部分似乎不正確。您需要NOT所有OR條件

if ((MyBeam.Position.Rotation == Position.RotationEnum.FRONT || MyBeam.Position.Rotation == Position.RotationEnum.BACK) && 
!((MyBeam.Profile.ProfileString.Contains("RHS") 
|| MyBeam.Profile.ProfileString.Contains("CHS") 
|| MyBeam.Profile.ProfileString.Contains("MET") 
|| MyBeam.Profile.ProfileString.Contains("SHS"))