闰年不适用于Dlang的日期和时间计划

时间:2015-09-03 07:09:15

标签: date time logic d

我还没有编程很长时间,所以我在dlang中练习一些逻辑练习。关于我在这里做错了什么的任何想法。当我进入闰年时,我的程序就会继续循环播放WHILE。

import std.stdio;
void main()
{
bool dead;
string thing;
int phew = 5; //days
int tahr = 1; //months
int tron; //monthsDate
string[7] days = ["Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday"];
int date = 28;
string[12] months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
int year = 1996;
int hours = 11;
int mins = 28;
string ampm = "pm";
bool ly;
int leap = 1996;
int cen = 996;
//writeln("This program is incomplete. Obviously.");
write("Press Enter to Continue.");
readf("%s\n",&thing);
while(!dead)
{
    while(hours <= 12)
    {
        while(mins <= 59)
        {
            if(mins < 10)
                write(date," ",months[tahr],", ",year,". ",days[phew],". ",hours,":0",mins,ampm,": ");
            else
                write(date," ",months[tahr],", ",year,". ",days[phew],". ",hours,":",mins,ampm,": ");
            readf("%s\n",&thing);
            mins++;
        }
        hours++;
        if(hours == 12 && ampm == "am")
        {
            ampm = "pm";
        }
        else if (hours == 12 && ampm == "pm")
        {
            ampm = "am";
            phew++;
            date++;
            if(phew > 6)
                phew = 0;
            if((date == 29 || date == 30) && tahr == 1)
            {
                while(leap <= year) //this assuming time travel doesn't work
                { //reminder: add time travel
                    if (leap == year)
                    {
                        ly = true;
                        break;
                    }
                    leap+=4;
                    ly = false;
                }
                if(!ly || date == 30)
                {
                    date = 31;
                    leap-=4;
                }
            }
                if(!ly || date == 30)
                {
                    date = 31;
                    leap-=4;
                }
            }
            if(date == 31 && (tahr == 1 || tahr == 3 || tahr == 5 || tahr == 8 || tahr == 10))
            {
                date = 1;
                tahr++;
            }
            else if (tahr == 11 && date == 32)
            {
                tahr = 0;
                date = 1;
                year++;
                cen++;
                if(cen == 1000)
                {
                    writeln("Happy Millennium!");
                    cen = 0;
                }
                else
                    writeln("Happy New Year!");
            }
            else if(date == 32 && (tahr == 0 || tahr == 2 || tahr == 4 || tahr == 6 || tahr == 7 || tahr == 9))
            {
                date = 1;
                tahr++;
            }
        }

        if(hours == 13)
        {
            hours = 1;
        }
        mins = 0;
    }
}
}

重要的是这个:

if((date == 29 || date == 30) && tahr == 1)
            {
                while(leap <= year) //this assuming time travel doesn't work
                { //reminder: add time travel
                    if (leap == year)///
                    {
                        ly = true;
                        break;
                    }
                    leap+=4;
                    ly = false;
                }
                if(!ly || date == 30)
                {
                    date = 31;
                    leap=-4;
                }
            }

所以,我在发布后几乎立即发现了这个问题。基本上,我写了= +而不是+ =。非常简单的错误。这就是我打字速度太快的原因。所以,我现在已修复了代码,如果您有任何其他建议,请务必将其放入评论中。感谢。

2 个答案:

答案 0 :(得分:2)

决定打印跳跃到终端,我想出了我的问题。 Leap持续等于4,因为我写了= +而不是+ =。这只是首先点击错误按钮的情况。这就是我快速输入的内容。据我所知,该计划现在正在运作。在你可能注意到的任何其他事情上随意纠正我。

答案 1 :(得分:0)

我没有深入阅读,但我建议您添加大量调试writelns。 特别是在块内部,您正在思考应该运行:

if((date == 29 || date == 30) && tahr == 1) { 
    writefln("Handling leap years...");
    while(leap <= year) //this assuming time travel doesn't work
    { //reminder: add time travel
        writefln("In loop, Leap <= year? %s", leap <= year);

这将允许您调试程序运行时实际发生的事情。