if statement not working right?

前端 未结 5 1651
执念已碎
执念已碎 2020-12-02 03:19

I\'ve looked and looked with the debugger and cannot seem to figure out why the IF statement always prints the message.

The IF statement checks to see if yesno != \'

5条回答
  •  既然无缘
    2020-12-02 03:53

    The || doesn't quite mean what you think it means. The correct approach is:

    if (yesno != 'Y' && yesno != 'N') { ...
    

    This evaluates each side of the && independently, and the result is true if both sides are true.

    Note that

    if (yesno != 'Y' || yesno != 'N') { ...
    

    will always be true because any given character is either not Y or it is not N. This is probably not what you want.

提交回复
热议问题