Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何修改if语句,使值在每一整秒钟后打印出来?_Java - Fatal编程技术网

Java 如何修改if语句,使值在每一整秒钟后打印出来?

Java 如何修改if语句,使值在每一整秒钟后打印出来?,java,Java,所以基本上,我有一个变量,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 va

所以基本上,我有一个变量,
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没有你想要的那种准确性,所以它不会像你的输出清楚地显示的那样,每次迭代都以0.01偶数计算。解决办法是使用。我重写了一点程序

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\u T
,不能准确地表示为
双精度值。每次迭代都会累积这个小错误,您可以在打印出来的
time
值中看到这一点

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

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

要么使用
Thread.sleep(1000L)
要么使用
ScheduledExecutorService
。伙计,我是新手!我不知道怎么用这些!我要去查一下!。每次可变时间是一个整数时,我都会打印出来。这是你学习的机会。我认为Javi指的是每一秒,而不是每一秒。
  final double PRINT_INTERVAL = 0.1;

  // Other code...

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