【发布时间】:2014-08-14 19:59:32
【问题描述】:
我刚刚学会了如何使用静态方法。我正在尝试使用以下代码计算每个行星的重力:
/**
* Calculates the acceleration due to gravity on each planet, and then displays the info in a nice table and in a text file
*
* @author Ely Eastman
* @version 14.8.2014
*/
public class GravityV1
{
//method for calulating gravity
public static double [] gravCalc(double [] d, double [] m){
double[] gravFinal = new double [d.length];
for(int i = 0; i < d.length; i++){
gravFinal[i] = ((6.67 * 10E-11) * m[i]) / Math.pow(((d[i] * 1000) / 2) , 2);
}
return gravFinal;
}
//method for printing to console
public static void printer(String [] s, double [] d, double [] m, double [] g){
System.out.println(" Planetary Data");
System.out.println(" Planet Diameter (km) Mass (kg) g (m/s^2) ");
System.out.println("------------------------------------------------------------");
for(int i = 0; i < s.length; i++){
System.out.printf(" %-7s%15.0f%17.2E%15.2f\n", s[i], d[i], m[i], g[i]);
}
}
//method to print to text file
public static void textCreator(){
//havent gotten to this method yet because it is essentially the same as the one above.
}
//main method
public static void main(String[] args){
//variables
double [] planetDiameter = {4880, 12104, 12756, 6794, 142984, 120536, 51118, 49352};
double [] planetMass = {3.30E23, 4.87E24, 5.97E24, 6.24E23, 1.9E24, 5.68E26, 8.68E25, 1.02E26};
double [] planetGrav = gravCalc(planetDiameter, planetMass);
String [] planetNames= {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
printer(planetNames, planetDiameter, planetMass, planetGrav);
}
}
一切正常,除了表中的重力列,所有内容都打印高十的幂,除了木星,打印低三的十幂。下面是我的输出图像,因为它不会在没有大量空格的情况下格式化。 output of program http://postimg.org/image/3oou8o8qt/3ab32a54/
我知道这可能是一个简单的错误,但在这个阶段,在一切都变成第二天性之前,让另一双眼睛看看我的代码会有所帮助。非常感谢您的帮助和耐心。
【问题讨论】:
-
您应该将当前输出添加到问题中。
-
为什么要乘以 1000?
-
@Nivas 将公里转换为米。
标签: java integer-division