[英]Why wont my if else statement work
The first two statements run with the given number input but the third statement doesn't run. 前两个语句以给定的数字输入运行,但第三个语句不运行。
#include <iostream>
using namespace std;
int main()
{
double X;
cout << "Please pick a number from -10 to 10";
cin >> X;
if ((-3 <= X)&&( X <= 2))
cout << "Y = " << ( X * X) + (2 * X) -3 << endl;
else if ((2 < X)&&(X <= 10))
cout << "Y = " << (5 * X) + 7 << endl;
else if ((X > 10)&&(X < -3))
cout << "Y = " << 0 << endl;
return 0;
}
When I choose -10
or 11
the result I receive is just an end program not a "Y = 0 ". 当我选择
-10
或11
,我收到的结果只是一个结束程序,而不是“ Y = 0”。
if ((X > 10)&&(X < -3))
At a time X
can't be X
> 10 and X
< -3 so it'll always return false. 一次
X
不能为X
> 10且X
<-3,因此它将始终返回false。
So it can never go inside (3rd) if
condition. 因此,
if
有条件,它永远不会进入内部(第3个)。
You might want to use ||
您可能要使用
||
instead of &&
so that if anyone is true it can go inside if
condition: 而不是
&&
因此如果任何&&
真,则可以在if
条件的情况下进入其中:
if ((X > 10)||(X < -3))
The answer is that one number cannot be greater then 10 and lower than -3 in one time. 答案是一个数字不能一次大于10且小于-3。 For example 20 is greater then 10 but also greater then -3.
例如20大于10,但也大于-3。 You can't find this number.
您找不到此号码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.