在声明cout << (i>0)
中,其中i
为正,且大于0,(? > 0)
总是有多大的影响(据我测试) 1?
#include <iostream>
using namespace std;
int main()
{
int i = 2, j = 5;
double k = 5.0;
cout << (i%j > 0) << " " << (i/k >0) << " " << (100>0) << endl;
cout << (i%j) << " " << (i/k) << endl;
return 0;
}
输出:
1 1 1
2 0.4
答案 0 :(得分:0)
(i>0)
返回布尔值true
/ false
,然后cout
自动将其输入1或0。
答案 1 :(得分:0)
您在perentheses中使用的表达式被评估为&#39; true&#39; 或&#39; false&#39; ,因此当表达式为评估为真输出为1,如果为假,则输出为0.如果需要,可以启用boolalpha,如下所示:
cout << boolalpha << (i % j > 0) << (i / k > 0);// rest of code...
这个的输出是:
true false
答案 2 :(得分:-3)
只需设置boolalpha:http://www.cplusplus.com/reference/ios/boolalpha/