更新两个数组以跟踪月份和天数

updating two arrays to keep track of months and days

本文关键字:跟踪 数组 两个 更新      更新时间:2023-10-16

我很难更新两个数组——下面的代码似乎只更新了两天。

int month[365], day[365];
int countMonths[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int i = 0;
int currentmonth = 0;
int currentday = 1;
while(i < 365 &&  i < countMonths[currentmonth])
{
    month[i] = currentmonth+1;
    day[i] = currentday;
    i++;
    currentday++;
if(currentday > countMonths[currentmonth]);
   {
    currentmonth++;
    currentday = 1;
   }
}

if语句上,您有一个额外的分号

if(currentday > countMonths[currentmonth]);

你不应该把它放在那里。

问题是while循环中有这样的条件:i < countMonths[currentmonth]-你停止迭代(因为i将是29,countMonths[1]是28),这就是为什么你的月份没有增加的原因。只要保持第一种状态,你就应该很好。