简体   繁体   English

我的单独方法不断返回错误的值

[英]My separate method keeps returning the wrong value

I'm using my weight as a value of 180 to find my weight in lbs on planets other than earth with a formula to find weight using mass and surface gravity. 我使用我的体重作为180的值来查找除地球以外的行星上的体重(以磅为单位),并具有使用质量和表面重力求重量的公式。 The problem I'm facing is that whenever I call this method, the weight value returns as 180 all 8 times the method is called. 我面临的问题是,无论何时调用此方法,权重值在调用该方法的8次时都将返回180。 In my code I have weight being calculated with (mass * roundedSG)/433... It shouldn't return as 180. I don't know how to fix this, I've been trying for a couple hours. 在我的代码中,我用(mass * roundedSG)/ 433计算出了权重...它不应返回180。我不知道如何解决这个问题,我已经尝试了几个小时。 The for loop is in a different method, I'm doing this for home work by the way, any help would be appreciated to just try and fix this problem of mine. for循环是另一种方法,顺便说一句,我正在做家庭作业,尝试解决此问题将对您有所帮助。 Thanks! 谢谢!

public static double weight(double sGravity, double w) 
{
    double roundedSG = sGravity / 10;
    double mass = (w * 433.59237)/roundedSG;
    double weight = (mass * roundedSG)/433.59237;

    return weight;
}

    for(int i = 0; i < planetSurfaceGravity.length; i++)
    {
        weightOnPlanets[i] = weight(planetSurfaceGravity[i], weightLbs);
        System.out.println(weightOnPlanets[i]);
    }
}

I think your math in weight() is incorrect. 我认为您的weight()数学不正确。 It looks like weight is actually returning the following: 看来weight实际上返回了以下内容:

 (w * 433/roundedSG)*roundedSG/433.

The 433s and roundedSGs cancel, and you're just returning w, which I'm guessing is 180? 433s和roundedSGs取消了,您只返回w,我猜是180?

Correct Weight Formula is: 正确的体重公式为:

weight = (mass*gravity); 重量=(质量*重力);

Change from: 更改自:

 double weight = (mass * roundedSG)/433.59237;

To: 至:

double weight = mass * roundedSG;

You weight method calculation are incorrect. 您的权重方法计算不正确。 the result of it returns only w which you pass as weightLbs (= 180). 结果仅返回w,您将其作为weightLbs(= 180)传递。

I renamed your variables and added a constant which makes the code as written a bit clearer: 我重命名了变量并添加了一个常量,使编写的代码更加清晰:

private static final double GRAMS_PER_POUND = 433.59237;
public static double weight(double sGravity, double weightLbs)  
{
    double roundedSG = sGravity / 10;
    double mass = (weightLbs * GRAMS_PER_POUND)/roundedSG;
    double weight = (mass * roundedSG)/GRAMS_PER_POUND;

    return weight;
}

So your mass on earth, which is converted to grams, is being divided by the gravity of whatever planet you're checking. 因此,您在地球上的质量(转换为克)将除以您要检查的任何行星的重力。 Change the mass calculation to be 将质量计算更改为

double mass = weightLbs * GRAMS_PER_POUND;

Then the weight calculation should be ok as is, returning the weight on the other planet converted back from grams to pounds. 然后按原样计算重量,将另一颗行星上的重量从克转换为磅。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM