在 java 中找到所有角度为 0 的时间

Find all the times which has angle 0 in java

我正在尝试打印所有时间,小时和分钟之间的角度为零。秒在这里也很重要。

我的输出是

0:0:0

12:0:0

,但我想要这样的输出

0:00,

1:05.45

public void FindZeroAngle ()
        {
            int seconds =00;
            int minute=00;
            int hour= 00;
            for(int i=0; i<86400 ;i++)
            {
                double h_angle= (30*hour)+ 0.5 *(minute+seconds/60.0);
                double m_angle =((6.0*minute)+(((1/60.0)*6)*seconds)); 
                double angle=m_angle-h_angle;

                if (angle<0) angle=-angle;
                if(Math.min(360-angle,angle)==0)
                {
                    System.out.println(hour + ":" + minute + ":" + seconds);
                }

                if(seconds !=60)
                    seconds++;

                if(seconds==60)
                {
                    seconds=0;
                    minute++;
                }

                if(minute==60)
                {
                    hour++;
                    minute=0;
                    if(hour==24)
                        hour=00;
                }

            }

我的代码有什么问题?

您仅以 1 秒为间隔进行采样。精确答案没有整数值秒,除了 00:00 和 12:00.

您已经认为您的问题与双精度和非精确答案有关。现在我想到的第一个解决方案类似于"Prune and Search"的概念。这个概念是检查从正面到负面的过渡 angular 差异,反之亦然。

详细:

Boolean hasIntersected = false;

[...] //Do some calculation of angle between minutes/hours. keep the sign, i.e. negative before intersection.

//Assuming that there's one intersection each hour, we reset the boolean every hour
if(minutes == 0 && seconds == 0)
    hasIntersected = false;

if(!hasIntersected && angle >= 0){
    hasIntersected == true;
    System.out.println("Output timestamp here or something");
}

angle >= 0 意思是在这个例子中分钟已经 passed/is 超过了小时,可以相反,这取决于你如何计算角度。

通过检查分钟数何时经过小时而不是检查它何时到达小时,您可以非常精确地进行,当您每秒 运行 时精确到秒。但是,如果它落在秒之间,它将向上舍入到下一秒。