[英]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.
我的问题特定于此程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.