简体   繁体   English

为什么此数组中的所有元素都具有相同的值?

[英]Why do all of the elements in this array have the same value?

I have a program designed to convert weight on Earth into mass into weight on other planets. 我有一个旨在将地球上的重量转换成质量再转换成其他行星上的重量的程序。 userWeight() , fileToken() , and printTable() are methods that are defined elsewhere in the program. userWeight()fileToken()printTable()是在程序其他位置定义的方法。

double earthWeight = userWeight();

// initialize arrays
String[] planet = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};

double[] gravity = new double[planet.length];
for(int index = 0; index < gravity.length; index++) {
    gravity[index] = Double.parseDouble(fileToken(index));
}

double[] mass = new double[planet.length];
for(int index = 0; index < mass.length; index++) {
    mass[index] = earthWeight * 453.59237 / (gravity[index] / gravity[2]);
}

double[] weight = new double[planet.length];
for(int index = 0; index < mass.length; index++) {
    weight[index] = mass[index] * (gravity[index] / gravity[2]) / 453.59237;
}

// print table
printTable(planet, gravity, weight);

All of the elements in weight have exactly the same value as earthWeight . weight所有元素的值与earthWeight They aren't supposed to be. 他们不应该是。

I think there is something wrong with this line: 我认为这行有问题:

weight[index] = mass[index] * (gravity[index] / gravity[2]) / 453.59273;

but I'm not sure. 但我不确定。

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

Note: This is not a duplicate of All elements of this array are the same, but why? 注意:这不是重复项。 此数组所有元素都相同,但是为什么呢? That person was writing in php, I am writing in Java. 那个人用php编写,我用Java编写。 My question is specific to this program. 我的问题特定于此程序。

This guy mentioned in comment, that you have a reversed formula. 这个人在评论中提到,您有一个相反的公式。 If you will pass the mass[index] formula instead of the result value, you will see that nothing exists anymore except for the weight of Earth. 如果您将传递mass[index]公式而不是结果值,则将看到除了地球的重量以外,什么都不再存在。 :) :)

![公式

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

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