以下 C++ 代码:
if (a != '0.5' || a != '0.6')
{
cout << "The answer is neither 0.5 nor 0.6 " << '\n';
}
我也试过
if ((a != '0.5') || (a != '0.6'))
{
cout << "The answer is neither 0.5 nor 0.6 " << '\n';
}
并尝试过
if ( !(a== '0.5') || !(a==0.6)
{
cout << "The answer is neither 0.5 nor 0.6 " << '\n';
}
从用户那里接收一个数字,并检查该数字是 0.5 还是 0.6;如果是,它应该作为假语句执行,但如果它是任何其他数字,它应该作为真执行。但是,尽管当我输入 0.5 或 0.6 时它应该执行为 false,但它仍然执行为 true。当我使用可以正常工作的 else if 语句时,这是不同的:
if (a != 0.5)
{
//what to do if true.
}
else if (a != 0.6)
{
//What to do if this is true and the first id wrong.
}
else
{
//What to do if none are true.
}
为什么 != 在 if 语句中不能执行?