编码基本数学公式

时间:2014-03-31 02:45:06

标签: java

我正在尝试编写一个扫描程序来询问用户钟摆的长度L,然后在几秒钟内找到T的周期。我基于下面的公式。

T = 2pi * sqrt(L / G)

其中我将G作为9.8的命名常量,L作为摆的长度。 这是我到目前为止所拥有的。谁能帮我吗? 当给定L时,我基本上试图解决T,并且知道G是9.8。

{
public static void main (String[] args) throws java.lang.Exception
{
import java.util.Scanner;
Public class Math
Scanner scan = new scanner(system.in);
System.out.print("Type the length of your pendulum: ");
double pendulum length = keyboard.nextdouble();
final double G = 9.8 meters per second,
             pi = 3.14159;
double powerTerm = Math.pow(pendulumLength, G, pi);
double periodOfpendulum = 2*pi*Math.sqrt(L/G);
System.out.println("The period of the pendulum is " +period+ " per second");
scan.close();

}
}

1 个答案:

答案 0 :(得分:1)

试试这个:

import java.util.Scanner;

public class Math {
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.print("Type the length of your pendulum: ");
    double L = scan.nextDouble();
    final double G = 9.8, PI = 3.14159;
    double period = 2*PI*java.lang.Math.sqrt(L/G);
    System.out.println("The period of the pendulum is " +period+ " per second");
    scan.close();

}
}

你的有几个问题。

> {
> public static void main (String[] args) throws java.lang.Exception { // main() should go after class declaration; why are you throwing an Exception?
> import java.util.Scanner; // import statements go at the very beginning (before class declaration)
> Public class Math // move before the main method
> Scanner scan = new scanner(system.in); // Upcase second scanner (needs to match the type "Scanner"; 'system' -> 'System'
> System.out.print("Type the length of your pendulum: ");
> double pendulum length = keyboard.nextdouble(); // where did you get "keyboard"?. Remove space from variable name
> final double G = 9.8 meters per second,pi = 3.14159; // meters per second doesn't belong; comment it out
> double powerTerm = Math.pow(pendulumLength, G, pi); // what is powerTerm used for? You never call it anywhere
> double periodOfpendulum = 2*pi*Math.sqrt(L/G); // rename class, or use java.lang.Math to disambiguate your class from the java.lang.Math class
> System.out.println("The period of the pendulum is " +period+ " per second"); // be consistent with variable names - periodOfpendulum not period. Not a syntactical issue, but shouldn't it be "period seconds" not "period per second"?
> scan.close();
> 
> } }

主要方法不能出现:

public class Math

将所有导入放在最开始位置。

我建议你把你的课叫做#34; Math",因为那已经是现有的Java库了,当调用像Math.pow这样的方法时,你需要指定java.lang.Math。 POW。

确保所有变量名称都一致。另外,要特别注意大写。

您的代码存在很多问题。如果您需要帮助理解我发布的代码,请发表评论。