[英]How can I make the variable from a subclass be used in main class
我需要完成从用户获取数据(距离和时间)并返回的方法
您正在调用此方法而没有赋值。
//Call the velocity Calculator method
velocityCalculator(distance, time);
double velocity=0;
之后,velocity为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());
读取double值double time = Double.parseDouble(br.readLine());
而不是Integer.parseInt
。
您应该从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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.