闰年运动。但是"否则"必须打印下一个闰年还有多少年

时间:2017-11-26 17:12:37

标签: c++ leap-year

所以我几乎刚刚开始编码。我想知道你们中的任何人是否可以帮我解决这个简单的代码。 我需要说明在下一个闰年还有多少年,我迷失了。

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;

}

1 个答案:

答案 0 :(得分: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;

}