首页 > 解决方案 > 如何将返回值从一种方法传递给另一种方法?

问题描述

我需要将 windCorrectionAngle 方法的返回值传递给 groundSpeed 方法,并将其放在表达式的最后(“windCorAng”)。这是唯一对我不起作用的部分。据我了解,这不是 Java 可以轻松完成的事情,因为它看不到其他方法的返回。很想学习如何做到这一点以及什么是正确的做法。我有一个简单的打印行来获得 grSpd 的结果。

public double windCorrectionAngle()
{
double windCorAng = Math.toDegrees(Math.asin 
( vw * Math.sin( Math.toRadians (w-d) ) / va) ); 
return windCorAng;
}

public double groundSpeed()
{
double grSpd = Math.sqrt( Math.pow(va,2) + Math.pow(vw,2) - 2 * va * vw * 
Math.cos(Math.toRadians(d - w - windCorAng))); 
return grSpd;
}

标签: methodsparametersreturn

解决方案


要在表达式中使用函数的返回值,只需在此处调用函数,即:

public double groundSpeed()
{
    return Math.sqrt( Math.pow(va, 2) + Math.pow(vw, 2)
                    - 2 * va * vw * Math.cos(Math.toRadians(d - w - windCorrectionAngle()))
                    ); 
}

推荐阅读