问题描述
我使用调试器查看并查看,并且似乎无法找出为什么IF语句总是打印该消息。IF语句检查是否yesno !='Y'||(或)'N'
但不管如果我键入y或Y或n N或HB它将显示...。我不知道什么动作,使得更多!我不能找到它的错误?
if(yesno!='Y'||'N'){。 ..
解决方案
我使用调试器查看并查看,并且似乎无法找出为什么IF语句总是打印该消息。IF语句检查是否yesno !='Y'||(或)'N'
但不管如果我键入y或Y或n N或HB它将显示...。我不知道什么动作,使得更多!我不能找到它的错误?
if(yesno!='Y'||'N'){。 ..
解决方案
||
并不意味着你的想法。正确的方法是:
if(yesno!='Y'&& yesno!='N' ..
这会评估&& $
请注意
if(yesno!='Y'|| yesno!='N'){...
会始终为真,因为任何给定的字符都是不是或不是N.这可能不是你想要的。
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 != 'Y' ||(or) 'N'but regardless if i type y or Y or n N or H B it will show... . Im not sure what move to make anymore! I cant seem to find where its going wrong?
if(yesno != 'Y' || 'N') { ...
Thanks guys.
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.
这篇关于如果语句不正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!