【问题标题】:Debug the issue in my escape velocity program在我的逃逸速度程序中调试问题
【发布时间】:2020-02-11 10:30:14
【问题描述】:

任务是编写一个输出的程序

  • 行星半径

  • 行星的质量

  • 逃逸速度

输入是周长和加速度。

有了 2 个输入,我们将使用

  1. 圆方程的周长计算半径,
  2. 通过重力方程计算质量的加速度
  3. 逃逸速度公式计算逃逸速度。

如果我的输入是 40075(地球周长)和 9.8(加速度),我的输出半径是 6378(正确),输出质量是 5.97e18(正确输出应该是 5.97e24),我的输出逃逸速度是 354 (正确的输出是 11184)。


这里是分配说明。

“使用下面的两个公式(一个给定,一个在链接中)

equation 1:
a=(G*m)/(r^2)

等式2:参考下面的链接

http://www.softschools.com/formulas/physics/escape_velocity_formula/90/

G 是一个常数(找到它)

向用户询问周长,以公里为单位

求重力加速度,单位为 m/s^2

输出:

  1. 以千米为单位的行星半径
  2. 以 kg 为单位的行星质量(使用公式 1)
  3. 以 km/s 为单位的逃逸速度(使用公式 2)

包括单位和格式"

这是我的程序代码。


import java.util.*;
import java.lang.Math;

class Main {
  public static void main(String[] args) {
    Scanner userInput = new Scanner (System.in);

    System.out.println("\nWelcome to the Escape Velocity Application. To begin, please enter the following information below. \nEnter the circumference (km):");
    double circum = userInput.nextDouble();

    System.out.println("Enter the acceleration due to gravity (m/s^2):");
    double a = userInput.nextDouble();

    //Gravitational constant 
    double G = 6.67408e-11;

    //Radius
    double r = Math.round((circum/2)/Math.PI);

    //Mass
    double m = Math.round((a*(Math.pow(r,2)))/G);

    //Escape Velocity
    double e = Math.round(Math.sqrt((2*G*m)/r));

    System.out.println("\nThe radius is: "+r+" kilometers.");
    System.out.println("\nThe mass is: "+m+" kg.");
    System.out.println("\nThe escape velocity is: "+e+" m/s.");

  }
}

【问题讨论】:

  • double r = Math.round((circum/2)/Math.PI); 应该是double r = Math.round((circum/2.0)/Math.PI);

标签: java repl.it


【解决方案1】:

经典物理错误!当您在物理中使用任何公式时,请确保您使用的是正确的单位。

您可以接受以千米为单位的天体周长输入,但请确保在计算期间将其转换为米。记住:x km = x *10^3m

double circum = 40075 * Math.pow(10, 3); // convert km to m

double f = 9.807; //more accurate

double G = 6.67408e-11;

double r = circum/(2*Math.PI);

double m = f*Math.pow(r, 2)/G;

double e = (Math.sqrt((2.0*G*(m))/r));

System.out.println("The radius is: " + r * Math.pow(10, -3) + " kilometers.");
System.out.println("The mass is: " + m + " kg.");
System.out.println("The escape velocity is: " + e + " m/s.");

这段代码给出了输出:

The radius is: 6378.134344407706 kilometers.

The mass is: 5.981328662579845E24 kg.

The escape velocity is: 11184.843630163667 m/s.

我所做的只是将 km 转换为 m 并将 f 更改为更准确的值。另请记住,在完成最终计算之前不要四舍五入,这样可以保持尽可能高的准确性。

【讨论】:

  • 谢谢!这很有帮助。我采纳了您的建议,并使用我当前的代码对其进行了修改,它可以正常工作!
  • 很高兴听到这个消息! @BipoN 很高兴我能提供帮助:)
猜你喜欢
  • 1970-01-01
  • 2015-06-10
  • 1970-01-01
  • 1970-01-01
  • 2017-06-23
  • 2012-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多