验证 C 语言的帮助
我正在尝试对输入的日期进行验证,从 1901 年 1 月 1 日到 2299 年 12 月 31 日,如果日期超出范围,请给出错误消息,这是我的代码,有些它无法正确验证,我我做错事了
int main (void)
{
// insert code here...
int day,month,year;
printf("Enter Year, Month and Day as YYYY,MM,DD\n");
scanf("4%d 2%d 2%d", &year, &month, &day);
if (year>1900 && year <2300) {
if (month>=1 && month <=12)
if (day >=1 && day <=31)
printf("correct/n");
else
printf("invalid/n");
}
return 0;
}
I'm trying to get this validation for a date entered, from Jan 01 1901 to Dec 31 2299, if the date is out of range, give a error message, this is my code, some how it doesn't validate correctly, am I doing something wrong
int main (void)
{
// insert code here...
int day,month,year;
printf("Enter Year, Month and Day as YYYY,MM,DD\n");
scanf("4%d 2%d 2%d", &year, &month, &day);
if (year>1900 && year <2300) {
if (month>=1 && month <=12)
if (day >=1 && day <=31)
printf("correct/n");
else
printf("invalid/n");
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是您正在使用嵌套的 if 语句,但没有足够的 else 语句。如果你简单地合并这些陈述,它应该有效。
The problem is that you're using nested if statements, but you don't have enough else statements. If you simply consolidate the statements it should work.
如果我这样做,我想我会从一个小函数开始,例如:
然后我会写一些类似的东西:
If I were doing this, I think I'd start with a small function like:
Then I'd write something like:
对于许多条件你什么也没说。修复:
当然,您可以折叠该代码以避免冗余。
我会将这个逻辑转移到一个函数中。
You don't say anything for a number of conditions. Fix:
Of course, you could collapse that code to avoid redundancies.
I'd move that logic into a function.