2

试图制定正确的 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
   }
4

1 回答 1

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 ))
于 2018-04-09T00:13:32.407 回答