有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

数学方程中的Java操作

我正试图编写一个程序来计算投射物的距离,但返回的距离并不正确。我熟悉Java中的运算符优先级,但不确定为什么没有得到正确的距离。对于角度=22,速度=35,高度=10,我期望得到75.54,但我得到的是42.03

我的代码中是否存在导致这种情况的明显错误

public class FootballDistanceCalculator {

    public static final double GRAVITATIONAL_ACCELERATION = 32.174;  
    /**
     * Calculates the distance a projectile travels
     * 
     * @param angle angle at which projectile is thrown in degrees
     * @param velocity initial velocity of projectile in miles/hour
     * @param height initial height of projectile in feet
     * @return distance traveled by projectile in feet
     */ 
    public static double calculateDistance(double angle, double velocity, double height) {
        double angleRadians = Math.toRadians(angle);
        double vCosineThetaOverG = (velocity * (Math.cos(angleRadians))) / GRAVITATIONAL_ACCELERATION ;
        double vSinTheta = velocity * (Math.sin(angleRadians));
        double vSinThetaSquared = (Math.pow(vSinTheta, 2));
        double twoGravHeight = (2 * GRAVITATIONAL_ACCELERATION * height);
        double radical = Math.sqrt((vSinThetaSquared + twoGravHeight));
        double distance = vCosineThetaOverG * (vSinTheta + radical);
        return distance;
    } 
}

这是我根据以下等式编制本程序:

d = (v cos(θ) / g)(v sin(θ) + √(v sin(θ)2 + 2 g h))

v = velocity
g = gravitational acceleration
h = height

正如评论中所指出的,这个问题是一个单位转换问题

我必须取我的速度参数,乘以每英里英尺数(5280),除以每小时秒数(3600),才能得到匹配的单位


共 (1) 个答案

  1. # 1 楼答案

    你是用公制计算的,但你的g常数是以每秒英尺数为单位的