2013-04-15 163 views
0

我的函数返回0?我的计算函数没有返回任何东西?

int ResizeLockRatio(int width, int height, int desired) 
{ 
    int returnValue = (height/width) * desired; 
    return returnValue; // returns 0? 
} 

int lockedRatioHeight = ResizeLockRatio(1920, 1200, 1440); 

任何想法?

回答

3
int returnValue = (height/width) * desired; 

你正在做整数除法,有时截断为0。

您正在通过width = 1920, height = 1200,因此height/widht =1200/1920在这种情况下整数除法将被截断为0。

编辑:您可以尝试先做乘法,然后根据“标题Obvlious”做师:

int returnValue = (height * desired) /width ; 
+0

AH CRAP啊..好感谢,这是它! BAHAHHA我是亲mannnn – Jimmyt1988

+0

如果你想坚持整数数学,我可以简单地投我的(浮)(高度/宽度) – Jimmyt1988

+2

@JamesT或'(高度*所需)/宽度'。 –

相关问题