请Java,在图表中打印计算结果?

时间:2013-11-09 20:17:29

标签: java printf

计划的总体目的:确定太阳系中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

非常感谢任何帮助!

2 个答案:

答案 0 :(得分: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]);

答案 1 :(得分: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