闰年在lang的日期和时间程序中不工作



我编程的时间不长,所以我在用lang做一些逻辑练习。你知道我哪里做错了吗?当我进入闰年时,我的程序只是在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("%sn",&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("%sn",&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;
                }
            }

所以,我几乎在发帖后立即发现了这个问题。基本上,我写了=+而不是+=。非常简单的错误。这就是我打字太快的结果。所以,我现在已经修复了代码,如果你有任何其他建议,请确保把它们放在评论中。谢谢。

决定将leap打印到终端,我找到了我的问题。Leap连续等于4,因为我写的是=+而不是+=。只不过是先按错了按钮而已。这就是我打字快的结果。据我所知,这个程序现在起作用了。

我还没有深入阅读它,但是我建议您添加大量的调试代码。特别是在你认为应该运行的代码块内:

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);

这将允许您调试更多实际发生的事情,当你的程序运行

相关内容

最新更新