或不等于 [英] Or and not equal

查看:79
本文介绍了或不等于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这为什么起作用?

#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;
} 

http://ideone.com/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;
}  

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

http://ideone.com/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";

这篇关于或不等于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆