我正在尝试验证除法残基。这是我的代码:
NSUInteger myNumber = 2;
// list is a NSArray
if ((arrayImg.count / ((float) imgPerPage)) % 1 >0)
{
// do something
}
但是我得到这个错误:
错误:对二进制表达式无效的操作数(“ float”和“ float”)
如果我这样做:
float result = (arrayImg.count / ((float) imgPerPage));
正常工作,但我不明白为什么我使用%1我得到了错误。
你们当中有人知道我的代码有什么问题吗?
非常感谢您的帮助
您只能对%
整数操作数使用模运算符(),因此首先将浮点表达式转换回整数。而且看起来您正在尝试测试奇/偶,因此您需要% 2
而不是% 1
。所以改变:
if ((arrayImg.count / ((float) imgPerPage)) % 1 >0)
至:
if (((int)(arrayImg.count / (float) imgPerPage)) % 2 > 0)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句