繁体   English   中英

如何修改我的if语句,以便每隔一秒钟输出一次值?

[英]How can I modify my if statement so that the values print out after every full second?

因此,基本上,我有一个变量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.

     }


    }




}

不好意思,只是我一直在尝试不同的上班方式,但到目前为止还没有找到。

问题是double并没有您想要的精度,因此每次输出都不会清楚地显示出每次迭代的偶数.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;
    }
}

问题在于您的时间步长DELTA_T不能精确表示为double DELTA_T值。 每次迭代都会累积这个小错误,您可以在打印出来的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
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM