My Java method won't compile [closed]
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).
精彩评论