或不等于

编程入门 行业动态 更新时间:2024-10-22 09:50:43
本文介绍了或不等于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这为什么起作用?

#include <iostream> #include <string> using namespace std; int main(){ string s="a"; if((s=="cm")||(s=="in")||(s=="ft")||(s=="m")) cout<<s+" Is an illegal value"; else cout<<"I like "+s; return 0; }

ideone/7pnYh

但是那不是

#include <iostream> #include <string> using namespace std; int main(){ string s="a"; if((s!="cm")||(s!="in")||(s!="ft")||(s!="m")) cout<<s+" Is an illegal value"; else cout<<"I like "+s; return 0; }

ideone/TXUXA 我想要的答案都是我喜欢".

ideone/TXUXA The answer i want for both is "I like a" .

推荐答案

在第二种情况下, s!="cm" ,当然还有第一个 cout .要取消检查并保持逻辑不变,请执行以下操作:

In your second case s != "cm" and of course the first cout is printed. To negate the check and the logic to remain the same, do it like this:

if((s!="cm")&&(s!="in")&&(s!="ft")&&(s!="m")) cout<<"I like "+s; else cout<<s+" Is an illegal value";

更多推荐

或不等于

本文发布于:2023-11-15 21:36:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1598164.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:或不

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!