简体   繁体   English

c ++简单if语句不检查?

[英]c++ simple if statements doesn't check?

i have a 'mouse reader' class which can turn rotations into x,y,z. 我有一个'鼠标阅读器'类,可以将旋转转换为x,y,z。 (It's a kind of first person camera) But i don't want the person to look directly at his axis, so i did this: (这是一种第一人称相机)但我不希望这个人直接看他的轴,所以我这样做:

if (!(Altitude  +  MouseCoordChange[1] > 269) && !(Altitude  +  MouseCoordChange[1] < 91))
{

I couted the checking and it's perfect, it prevents you from going too much up and down, but some times, it goes under 91 and 269, how is this possible? 我支付了检查,这是完美的,它可以防止你上下太多,但有时候,它会低于91和269,这怎么可能?

Thank You 谢谢

You've fallen foul of De Morgen's Law 你已经违反了德摩根定律

in other words: 换一种说法:

!A && !B = !(A || B) !A &&!B =!(A || B)

I suspect what you wanted was: 我怀疑你想要的是:

if (!((Altitude  +  MouseCoordChange[1] > 269) || 
      (Altitude  +  MouseCoordChange[1] < 91)))
{

Or even clearer: 甚至更清楚:

if ((Altitude  +  MouseCoordChange[1] <= 269) && 
    (Altitude  +  MouseCoordChange[1] >= 91)))
{

Ok, just for debugging, Try this inverse logic. 好的,只是为了调试,试试这个逆逻辑。

if (  (Altitude  +  MouseCoordChange[1] < 91) &&  (Altitude  +  MouseCoordChange[1] > 269)  )
{

  //Do nothing here
}
else
{

  //Do stuff here

}

Range-checking happens quite often so a template will be useful for that: 范围检查经常发生,因此模板对此有用:

template< typename _T > bool inrange(_T first, _T last, _T value)
{
    return ((value>=first) && (value<=last));
}

In your case: 在你的情况下:

if (inrange(91, 269, (Altitude  +  MouseCoordChange[1]))
{
    //is in the range - do something
}
else
{
    //is outside the range - do something
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM