当我尝试使用方法gravity2时,我得到零输出,我仍然不完全了解分离方法如何帮助,因为我不熟悉不同方法的规则。我如何调用该方法以使q[]
打印?
public class Gravity
{
public static double[] gravity(double[] radius, double[] z, double[] radius1)
{
for (int index = 0; index < radius.length; index++)
{
z[index] = radius[index] * radius1[index];
}
return z;
}
public static double[] gravity2(double[] q, double[] masses, double[] z)
{
for (int index = 0; index < masses.length; index++)
{
q[index] = (z[index] * 6.67E-11) / masses[index];
}
return z;
}
public static void printResults(String[] names, double[] z, String[] m, double[] q) throws IOException
{
System.out.println(" Plantetary Data");
System.out.println("=====================================");
System.out.println("Planet Diameter Mass Gravity");
for (int index = 0; index < names.length; index++)
{
System.out.printf("%1s ", names[index]);
System.out.printf("%7.1f", z[index]);
System.out.printf("%10s", m[index]);
System.out.printf("%6.1f", q[index]);
System.out.printf("%n");
}
}
public static void main(String[] args) throws IOException
{
// Initialize variables
String[] names =
{
"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"
};
double[] radius =
{
2439.7, 6051.9, 6378, 3402.5, 71492, 60270, 25562, 24774, 1195
};
double[] masses =
{
3.3022 * Math.pow(10, 23), 4.8685 * Math.pow(10, 24), 5.9736 * Math.pow(10, 24), 6.4185 * Math.pow(10, 23), 1.8986 * Math.pow(10, 27), 5.6846 * Math.pow(10, 26), 8.6810 * Math.pow(10, 25), 1.0243 * Math.pow(10, 26), 1.312 * Math.pow(10, 22)
};
8.68E+25
, 1.02E+26, 1.27E+22}; // See IMACS double lesson for big E notation
double[] radius1 =
{
2439.7, 6051.9, 6378, 3402.5, 71492, 60270, 25562, 24774, 1195
};
double z[] = new double[10];
double q[] = new double[10];
String m[] =
{
"3.30E+23", "4.87E+24", "5.97E+24", "6.42E+23", "1.90E+27", " 5.68E+26", " 8.68E+25", "1.02E+26", " 1.27E+22"
};
// Processing
double gravities1[] = gravity(radius, z, radius1);
double gravities2[] = gravity2(q, masses, z);
// Output
printResults(names, z, m, q);
printToFile(gravities1);
printToFile(gravities2);
}
答案 0 :(得分:0)
问题是q
中的值非常小,大小为10e-27,因此当您尝试使用如此少的小数位数进行打印时,它会显示0.更改为如下所示:< / p>
System.out.printf("%g ", q[index]);
你会得到:
Plantetary Data
=====================================
Planet Diameter Mass Gravity
Mercury 5952136.1 3.30E+23 1.20225e-27
Venus 36625493.6 4.87E+24 5.01781e-28
Earth 40678884.0 5.97E+24 4.54212e-28
Mars 11577006.3 6.42E+23 1.20306e-27
Jupiter 5111106064.0 1.90E+27 1.79559e-28
Saturn 3632472900.0 5.68E+26 4.26215e-28
Uranus 653415844.0 8.68E+25 5.02049e-28
Neptune 613751076.0 1.02E+26 3.99660e-28
Pluto 1428025.0 1.27E+22 7.25985e-27