我刚学会了如何使用静态方法。我试图用这段代码计算每个星球的引力:
/**
* 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/
我知道它可能是一个简单的错误,但是在这个阶段,只有让第二组眼睛看到我的代码才会变成第二天性。非常感谢你的帮助和耐心。
答案 0 :(得分:5)
你的两个常量都没有了,否则你的代码就可以了。
与其他行星相比,木星的质量看起来很低。 This page将其列为1.90e27 kg,而不是1.90e24 kg - 您的罚款是1,000。
引力常数偏离10
因子,因为您使用10e-11
代替1e-11
作为常量的指数部分。如果您愿意,可以使用一个double
字面值作为引力常数。
6.67E-11