【问题标题】:Leap year exercise. but "else" has to print how many years there is left to next leap year闰年运动。但是“else”必须打印下一个闰年还有多少年
【发布时间】:2017-11-27 01:34:35
【问题描述】:

所以我几乎刚刚开始编码。我想知道你们中是否有人可以帮助我编写这个简单的代码。 我需要说明距离下一个闰年还有多少年,我迷路了。

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;

}

【问题讨论】:

标签: c++ leap-year


【解决方案1】:

我修改了你的代码。请使用以下代码并尝试理解您的错误:-

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;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多