问题描述
- 【C 语言】 这个代码错在哪了?我用的是Dev C++
-
/*题目:输入某年某月某日,判断这一天是这一年的第几天?*/
#include
#define MONTH 12
struct date{
int Day;
int Month;
int Year;
};
int main()
{
int count,days,month;
struct date today;
int daysPerMonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};printf("请输入日期(如:2015.10.1):"); scanf("%i.%i.%i", &today.Year, &today.Month, &today.Day); printf("您输入的日期是:%i.%i.%in", today.Year, today.Month, today.Day); while ( today.Month > MONTH || today.Day > daysPerMonth[today.Month + 1] || today.Month < 1 || scanf("%i.%i.%i", &today.Year, &today.Month, &today.Day) != 3) // 输入:2015.11.6.3 为什么会出错! { printf("请输入正确的日期(如:2015.10.1):"); scanf("%i.%i.%i", &today.Year, &today.Month, &today.Day); } if ((today.Year%4 == 0 && today.Year%100 != 0 ) || today.Year%400 == 0) { daysPerMonth[1]=29; } for ( month=1; month <= today.Month; month++) { for ( days=1,count=1; days <= daysPerMonth[today.Month+1]; days++) { count ++; } } printf("今天是今年的第%d天", count); return 0;
}
本人刚学编程不久,希望各位可以解答详细点,拜托各位了!非常感谢!
问:
1.代码问题出在哪里了,怎么修改这个代码?2.在输入2015.11.6.3(最后一位任意值都可)为什么会出错?怎样解决这个问题?
解决方案
为什么多输入呢,必须对应的
解决方案二:
把while里面的scanf去掉,在前面定义一个ret = scanf
然后在while里用ret来判断是否为3
时间: 2024-09-11 06:52:18