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

My calculation function is not returning anything?

本文关键字:返回 任何东 计算 函数 我的      更新时间:2023-10-16

我的函数返回0?

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

任何想法?

 int returnValue = ( height / width ) * desired;

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

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

EDIT:你可以尝试先做乘法再做除法,根据"Caption visible ":

int returnValue = ( height * desired ) /width ;