闰年练习。但是 "else" 必须打印下一个闰年还剩多少年
Leap year exercise. but "else" has to print how many years there is left to next 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;
}
我已经修改了你的代码。请使用以下代码并尝试理解您的错误:-
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;
}
所以我几乎才刚刚开始编码。我想知道你们中是否有人可以帮助我处理这个简单的代码。 我需要说明离下一个闰年还有多少年,我迷路了。
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;
}