如果条件1或条件2和条件3或条件4,则为lang

时间:2018-04-08 19:45:01

标签: microchip xc16

尝试制定正确的if条件测试。我想只在两个楼层锁在线时才采取行动(不报告eStatusLockUnknown)。我试过的两个测试如下。在调试时,我可以看到两个楼层锁都在报告*.lockState = eStatusLockIndeterminate,但我没有涉及我的操作代码。 Microchip xc16 v1.25编译器。

if (( sLockStatus[eHeadFloorLock].lockState == ( eStatusLockIndeterminate 
|| eStatusLockEngaged || eStatusLockStowed || eStatusLockFullyEngaged ))
   && ( sLockStatus[eFootFloorLock].lockState == ( eStatusLockIndeterminate || eStatusLockEngaged || eStatusLockStowed || eStatusLockFullyEngaged )))
{
    // take action
}

if (( sLockStatus[eHeadFloorLock].lockState == eStatusLockIndeterminate )
     || ( sLockStatus[eHeadFloorLock].lockState == eStatusLockEngaged )
     || ( sLockStatus[eHeadFloorLock].lockState == eStatusLockStowed )
     || ( sLockStatus[eHeadFloorLock].lockState == eStatusLockFullyEngaged )
   && ( sLockStatus[eFootFloorLock].lockState == eStatusLockIndeterminate )
     || ( sLockStatus[eFootFloorLock].lockState == eStatusLockEngaged )
     || ( sLockStatus[eFootFloorLock].lockState == eStatusLockStowed )
     || ( sLockStatus[eFootFloorLock].lockState == eStatusLockFullyEngaged ))
   {
      // take action
   }

1 个答案:

答案 0 :(得分:1)

首先是错误的,因为你在常数上做||而不是在条件上。

第二个是错误的,因为&&||绑定得更紧密。您需要围绕最外||条件的括号。

以下应该做你想做的事。

LockState headLockState = sLockStatus[eHeadFloorLock].lockState;
LockState footLockState = sLockStatus[eHeadFloorLock].lockState;

if (( headLockState == eStatusLockIndeterminate
     || headLockState == eStatusLockEngaged
     || headLockState == eStatusLockStowed
     || headLockState == eStatusLockFullyEngaged )
   && ( footLockState == eStatusLockIndeterminate
     || footLockState == eStatusLockEngaged
     || footLockState == eStatusLockStowed
     || footLockState == eStatusLockFullyEngaged ))