【发布时间】:2015-11-02 13:33:53
【问题描述】:
当我将两个整数相除时,我试图确定结果是否为整数。我该怎么做?
float result;
int number1, number2;
cout<<"Give me number 1 and 2"<<endl;
cin>>number1;
cin>>number2;
result = number1 / number2;
if(result == int)
{
cout<<"The numbers can be devided"<<endl;
}else cout<<"The numbers can't be devided"<<endl;
我出错了,因为你不能那样做,有人可以帮帮我吗?这让我很烦,每篇文章都在告诉我,如何检查输入是否为整数,而不是结果。我不关心输入,我想知道结果是否为整数。感谢您的帮助!
编辑:
所以我的函数看起来像这样:
int funkcijaDva()
{
int popizdobom = 0;
int tri = 3;
int tristo = 300;
int tritisoc = 3000;
float result;
for(; tristo!=tritisoc;tristo++)
{
result = (int)tristo / tri;
if(tri % tristo == 0)
{
popizdobom++;
}
}
return popizdobom;
}
popizdobom 的结果总是 0...我做错了什么吗? 编辑: 让它工作......我猜编译器有问题......
【问题讨论】:
-
写100次:“C is not C++ is not C!”。
-
两个
ints 的操作结果将永远是int -
这实际上是一个合法的问题,因为我班上没有人知道如何解决它,所以不要对它这么消极......
-
“我班上没有人......”在这里不合法。但是,问题不在于问题本身,而在于您没有提供信息。
-
我不明白为什么这个问题有这么多反对票?如果你不喜欢这个问题,或者你认为它太基本,那就不要回答它......如果你这样做,那么对你的反对票给出适当的解释......