我需要完成从用户获取数据(距离和时间)并返回
的方法答案 0 :(得分:3)
您正在调用此方法而没有赋值。
//Call the velocity Calculator method
velocityCalculator(distance, time);
double velocity=0;
之后,速度为0,您打印0。
您必须在方法中返回一个值,并将其分配给您的变量,如下所示:
public static double velocityCalculator(double distance, double time) {
return distance/time;
}
并在您的主要内容中执行以下操作:
//Call the velocity Calculator method
double velocity = velocityCalculator(distance, time);
现在,您的方法velocityCalculator
将返回计算出的值并将其分配给新创建的变量velocity
。
另一点是你想用浮点数计算,但你只是读整数。您可以使用double time = Double.parseDouble(br.readLine());
而不是Integer.parseInt
读取双倍值。
答案 1 :(得分:0)
你应该从velocityCalculator函数返回速度值,或者将速度变量声明为全局,并重新赋值。
最好,返回它的值是最好的方法,所以修改你的函数调用
//Call the velocity Calculator method
double velocity= velocityCalculator(distance, time);
并在您的计算函数中返回其值
public static double velocityCalculator(double distance, double time)//this subroutine will calculate the velocity and print it
{
double velocity = distance/time;
//calculates the velocity
return velocity;
}//closes velocityCalculator method