0

所以基本上,我有一个变量 ,time并且希望程序每整秒打印一次其他值。

例如,如果我插入 100,它应该只打印 20 秒。

import java.util.Scanner;
public class CannonBlaster {

public static void main(String[] args) {


    Scanner input=new Scanner(System.in);
     final double DELTA_T = 0.01; //initiating all variables
     final double G = 9.81; 
     double s = 0.0; 
     double time = 0.0; 
     double second = 0;


    System.out.println("What's the initial velocity?: ");//asking for the initial velocity
    double v =input.nextDouble();


    while (s >= 0.0) //while loop is used. As long as the height isn't negative it will continue to go.
    {

    s += v * DELTA_T; //demonstrates the change of velocity and position for every .01 second.
    v -= G * DELTA_T; 
    time += DELTA_T; 

    System.out.println("The time is: "+time+" "+(double) Math.floor(time)+" "+Math.round(time * 1000) / 1000);
    second=Math.round(time * 1) / 1;
    if ((double) Math.floor(time) ==time)
     {
       System.out.println("Approximated position: "+ s); 
       System.out.println("Formula's position: "+(100.0 * time - (time*time * G) / 2.0)); //prints out the formula values and the loop values.

     }


    }




}

对不起,这只是我一直在尝试不同的方式去上班,但到目前为止还没有找到。

4

2 回答 2

0

问题是您的时间步DELTA_T长 不能完全表示为一个double值。每次迭代都会累积这个小错误,您可以在time打印出来的值中看到这一点。

通常,在比较两个浮点数时,最好通过将两个数字之间的绝对差与某个“小”值进行比较来避免这个问题,其中“小”是由您正在使用的数字的问题/大小定义的。 DELTA_T在这里非常适合,因此您可以将此比较用于每秒时间步长:

  if (Math.abs(time - Math.round(time)) < DELTA_T)
  {
     // Other code here
  }

或者,对于更广义的时间步长,在PRINT_INTERVAL

  final double PRINT_INTERVAL = 0.1;

  // Other code...

  if (Math.abs(time / PRINT_INTERVAL - Math.round(time / PRINT_INTERVAL)) < DELTA_T)
  {
     // Other code here
  }
于 2013-09-30T23:34:17.857 回答
0

问题是 double 不具有您正在寻找的那种准确性,因此它不会像您的输出清楚地显示的那样每次迭代都计数为 0.01。解决方案是使用BigDecimal。我稍微重写了程序...

package test;

import java.math.BigDecimal;
import java.util.Scanner;

public class CannonBlaster {

    private static final double G = 9.81;
    private static final BigDecimal DELTA_T = new BigDecimal(0.01);
    private static final double DELTA_T_DOUBLE = DELTA_T.doubleValue();

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        double s = 0.0;
        BigDecimal time = new BigDecimal(0.0);
        double time_double = 0.0;

        System.out.println("What's the initial velocity?: ");// asking for the
                                                                // initial
                                                                // velocity
        double v = input.nextDouble();

        // As long as the height isn't negative it will continue to go.
        while (s >= 0.0)
        {

            s += v * DELTA_T_DOUBLE;
            v -= G * DELTA_T_DOUBLE;
            time = time.add(DELTA_T);
            time_double = time.doubleValue();
            if (time.doubleValue()%1==0) {
                System.out.printf("Approximated position at t=%3ds is %10.6f.\n", time.intValue(), s);
                // prints out the formula values and the loop values.
                System.out.println("Formula's position: " + formula(time_double)); 

            }

        }
    }

    private static double formula(double x){
        return 100.0 * x - (x * x * G) / 2.0;
    }
}
于 2013-09-30T23:23:50.153 回答