gpt4 book ai didi

java - 请问,Java,在图表中打印计算结果吗?

转载 作者:行者123 更新时间:2023-11-30 04:09:54 26 4
gpt4 key购买 nike

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

非常感谢任何帮助!

最佳答案

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

关于java - 请问,Java,在图表中打印计算结果吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19882571/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com