调试逃生速度程序中的问题

问题描述 投票:0回答:1

任务是编写输出程序

  • 行星半径

  • 行星的质量

  • 逃逸速度

输入是周长和加速度。

[使用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. 以千克为单位的行星质量(使用公式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.");

  }
}
java repl.it
1个回答
0
投票

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

您可以接受以千米为单位的天体周长的输入,但请确保在计算过程中将其转换为米。记住: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转换为更准确的值。

© www.soinside.com 2019 - 2024. All rights reserved.