2013-04-15 76 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或'(高度*所需)/寬度'。 –

相關問題