开发者

My Java method won't compile [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 10 years ago.

I have a problem with my Java method. It doesn't seems to be compiling correct, and I can't figure out what is wrong.

I hope someone can help me check this method for errors, so I don't get an compiling error.

/**
 * Calculate the speed in Kilometers per hour.
 * @param lentgh The length drove in kilometers.
 * @param time The time used in minutes.
 * @return The speed in the datatype integer.
 */
public static int getSpee开发者_Go百科d(double length, double time)
{
    return (length/(time/60));
}


You have to cast the result to int:

return (int)(length/(time/60));

If you want to round instead, use Math.round:

return (int) Math.round(length/(time/60));

Note that you should check whether the speed actually fits in the size of an integer (the case doesn't raise an exception).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜