【问题标题】:Math result is off by powers of ten数学结果是十的幂
【发布时间】: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


【解决方案1】:

你的两个常量是关闭的,否则你的代码是好的。

与其他行星相比,木星的质量看起来很低。 This page 将其列为 1.90e27 公斤,而不是 1.90e24 公斤 - 你相差了 1,000 倍。

引力常数偏离了10 的因子,因为您使用10e-11 而不是1e-11 作为常数的指数部分。如果您愿意,可以使用一个 double 文字作为引力常数。

6.67E-11

【讨论】:

  • 所以我的代码是对的!啊哈。没有什么比这更好的了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-26
  • 2016-07-29
  • 2013-03-16
  • 2011-02-02
  • 1970-01-01
  • 1970-01-01
  • 2020-03-07
相关资源
最近更新 更多