#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;
int main ()
{
double x = 10000;
double y = x*0.05;
if(500 == 400+100)
cout<<"asd1"<<endl;
if(500 == 0.05*x)
cout<<"asd2"<<endl;
if(500 == y)
cout<<"asd3"<<endl;
}
上面的代码应该打印所有三个asd1
、asd2
、asd3
。正如预测的那样,Visual Studio 打印了所有三个。但是,我的代码块不会。它没有打印 asd2
。我该如何解决这个问题?
最佳答案
将其更改为:
int main ()
{
int x = 10000;
int y = x*0.05;
if(500 == 400+100)
cout<<"asd1"<<endl;
if(500 == int(0.05*x))
cout<<"asd2"<<endl;
if(500 == y)
cout<<"asd3"<<endl;
}
并尝试阅读:http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
关于c++ - 代码块中的奇怪逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33428881/