闰年锻炼。但是"else"必须打印到下一个闰年还剩多少年

Leap year exercise. but "else" has to print how many years there is left to next leap year

本文关键字:闰年 下一个 多少年 打印 但是 else      更新时间:2023-10-16

,所以我几乎刚刚开始编码。我想知道你们中的任何一个都可以帮助我使用这个简单的代码。我需要说明直到下一个leap年还剩多少年,我迷路了。

int main()
{
int year;
cout << "Enter a year: ";
cin >> year;
if (year % 4 == 0)
{
    if (year % 100 == 0)
    {
        if (year % 400 == 0)
            cout << year << " is a leap year.";
        else
            cout << "There is " << 4%-year << " years till next leap year";
    }
    else
        cout << year << " is a leap year.";
}
else
   cout << "There is " << ???year << " years till next leap year";
return 0;

}

我修改了您的代码。请使用以下代码进行相同的代码,并尝试了解您的错误: -

int main()
{
    int year;
    cout << "Enter a year: ";
    cin >> year;
    int isleap = year % 4;
    if (isleap == 0)
    {
        isleap = year % 100;
        if (isleap == 0)
        {
            isleap = year % 400;
            if (isleap == 0)
                cout << year << " is a leap year."<<endl;
            else
                cout << "There is " << (4-isleap) << " years till next leap year"<<endl;
        }
        else
            cout << year << " is a leap year."<<endl;
    }
    else
       cout << "There is " << (4-isleap) << " years till next leap year"<<endl;
    return 0;
}