1

程序的总体目的:确定我们太阳系中 9 颗行星中每颗行星的表面重力 (g)。

(我知道,我知道……冥王星不是行星,但它在这个程序中)。

问题: 程序创建图表并计算表面重力。我需要帮助使格式正确 - 它应该在列中排列。同样在尝试解决此问题的过程中,我以某种方式弄乱了程序,以便它仅打印其中一个行星的计算表面积。

import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;

public class GravityV1
{   

   //print the data
   public static void printResults(String[] names, double[] diameter, double[] mass, int x);
   {
      System.out.printf("%-10s%21.1f%20.4e\n",names[x],diameter[x],mass[x]);          
   }

   //calculate the surface gravity
   public static double calcGravity(double[] m, double[] d)
   {
    double gravity = 0.0;

      for (int i= 0; i < 9; i++) {
        gravity = (((6.67E-11) * (m[i])) / (Math.pow(d[i] / 2, 2)));
      }

    return gravity;        
   }


   //main method
   public static void main (String [ ] args)
   {        
     double[] mass = { 3.30E23, 4.869E24, 5.972E24, 6.4219E23, 1.900E27, 5.68E26, 8.683E25, 1.0247E26, 1.27E22 };

     double[] diameter = { 4880000, 12103000.6, 12756000.3, 6794000, 142984000, 120536000, 51118000, 49532000, 2274000 };

     String[] names = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto" };


     System.out.printf("%-7s%20s%20s%25s\n","Planet Name","Diameter","Mass(kg)", "g (m/s^2)");    System.out.println("=============================================================================");

       for (int x = 0; x < 9; x++) {

         printResults(names, diameter, mass, x);

         double surfaceGravity = calcGravity(mass, diameter);

         System.out.println(surfaceGravity);                        
      }  

  System.out.println();
}

}

输出现在看起来像这样:

Planet Name            Diameter            Mass(kg)                g (m/s^2)
=============================================================================
Mercury               4880000.0          3.3000e+23
0.655252407816091
Venus                12103000.6          4.8690e+24
0.655252407816091
Earth                12756000.3          5.9720e+24
0.655252407816091
Mars                  6794000.0          6.4219e+23
0.655252407816091
Jupiter             142984000.0          1.9000e+27
0.655252407816091
Saturn              120536000.0          5.6800e+26
0.655252407816091
Uranus               51118000.0          8.6830e+25
0.655252407816091
Neptune              49532000.0          1.0247e+26
0.655252407816091
Pluto                 2274000.0          1.2700e+22
0.655252407816091

任何帮助深表感谢!

4

2 回答 2

1

您的calcGravity方法计算所有行星的表面重力,并返回最后一个。而不是这样做,它可能应该只计算一个特定行星的表面重力:

public static double calcGravity(double m, double d)
{
 double gravity = (((6.67E-11) * (m)) / (Math.pow(d / 2, 2)));
 return gravity;        
}

然后您将使用特定行星的数据调用此方法,而不是使用所有行星的数据:

double surfaceGravity = calcGravity(mass[x], diameter[x]);
于 2013-11-09T20:24:04.237 回答
1
System.out.printf("%-10s%21.1f%20.4e",names[x],diameter[x],mass[x]); // removed 2 characters

System.out.printf("%.21f\n", surfaceGravity); // play with number to get it right
于 2013-11-09T20:28:00.790 回答