2017-01-14 5 views
-2

私はいくつかの計算をしようとしていましたが、何かがかなり足りません。私は expected数学計算のためにJAVAループを使用

下のスクリーンショットを達成しようとしていますが、これは、私はいくつかの助けを必要としてください

result

を得ているものです、これは私がこれまで

public class VelocityFall 
{ 
public static void main (String [] a) 
{ 
    Scanner s = new Scanner (System.in); 
    System.out.print("This program prints a table that shows each \nsecond,"  
    + 
    "height from the ground (meters), and the velocity (m/s)\n of a free-falling" + 
    "object from an initial height (metres).\nPlease input the Initial Height H: "); 


    // input/get the value of H from the keyboard 
    double H = s.nextDouble(); 
    // we need to design/output the table by using println with lines and tabs (\t) 

    System.out.println ("------------------------------------------"); 
    System.out.println (" t(s)\t\tHeight(m)\t\tVelocity(m/s)"); 
    System.out.println ("------------------------------------------"); 

    //we now require a for loop 
    for (int t = 0; t<=15; t++) 
    { 
    // we are now going to calculate and output the velocity and decreasing 
    height 
    double velocity = 9.8*t; 
    H = H-(0.5*9.8*Math.pow(t,2)); 
    System.out.println(t + "\t\t" + H + "\t\t" + velocity); 

    } 
    } 
} 
をやっていることです
+1

私は運動学を取ってからずっと前ですが、前の高さからではなく、最初の高さからHを計算してはいけませんか? –

+0

あなたの質問を編集してより良いフォーマットにすることができますか?あなたがしようとしていることを説明してください*。 「いくつかの計算」は単なるゆるやかな言葉であり、正確な計算は何をしようとしているのか、どの入力とどのように出力が異なるのか、そして非常に重要なことです。それを修正しようとしましたが、それを動作させることに終わったのですか? –

答えて

2

問題は、下の行にH変数を再割り当てすることです。

H = H-(0.5*9.8*Math.pow(t,2)); 

正しい出力を得るには、その行を次の行に置き換えます。

double H_new = H-(0.5*9.8*Math.pow(t,2)); 

はあまりにもあなたのprintlnコールで変数を変更することを忘れないでください:

System.out.println(t + "\t\t" + H_new + "\t\t" + velocity); 

この方法は、H変数は、ユーザの入力に等しいままで、あなたの計算は影響を受けません前回の計算の結果より


出力:

t(s)  Height(m)  Velocity(m/s) 
------------------------------------------ 
0  1234.56  0.0 
1  1229.6599999999999  9.8 
2  1214.96  19.6 
3  1190.46  29.400000000000002 
4  1156.1599999999999  39.2 
5  1112.06  49.0 
6  1058.1599999999999  58.800000000000004 
7  994.4599999999999  68.60000000000001 
8  920.9599999999999  78.4 
9  837.6599999999999  88.2 
10  744.56  98.0 
11  641.6599999999999  107.80000000000001 
12  528.9599999999999  117.60000000000001 
13  406.4599999999999  127.4 
14  274.15999999999985  137.20000000000002 
15  132.05999999999995  147.0 

繰り返し桁の問題については、DecimalFormatクラスを使用してみてください。

関連する問題