关于switch中的default,为什么我定义的default不起作用啊
#include<stdio.h>void main()
{
int year,month,day;
int leap;
printf("please input year,month,day:");
scanf("%d%d%d",&year,&month,&day);
switch(month)
{
case 12:
day+=30;
case 11:
day+=31;
case 10:
day+=30;
case 9:
day+=31;
case 8:
day+=31;
case 7:
day+=30;
case 6:
day+=31;
case 5:
day+=30;
case 4:
day+=31;
case 3:
day+=28;
case 2:
day+=31;
case 1:
day=day;
default:
printf("please input error:\n");
break;
}
if((year%400==0 || year%4==0 &&year%100!=0) && month>2)
leap=1;
else
leap=0;
if(leap=1 && month>2)
day++;
printf("\nday=%d\n",day);
}
----------------解决方案--------------------------------------------------------
/* 12个月的天数因该放在数组中 */
因该使用数组
[[italic] 本帖最后由 cosdos 于 2007-11-27 00:02 编辑 [/italic]]
----------------解决方案--------------------------------------------------------
/* switch 的修改 */
switch(month)
{
case 12: day+=30;
case 11: day+=31;
case 10: day+=30;
case 9: day+=31;
case 8: day+=31;
case 7: day+=30;
case 6: day+=31;
case 5: day+=30;
case 4: day+=31;
case 3: day+=28;
case 2: day+=31;
case 1: day=day; break;
default: printf("please input error:\n"); break;
}
----------------解决方案--------------------------------------------------------
回复 1# 的帖子
#include<stdio.h>void main()
{
int year,month,day;
int leap;
printf("please input year,month,day:");
scanf("%d%d%d",&year,&month,&day);
switch(month)
{
case 12:
day+=30;
case 11:
day+=31;
case 10:
day+=30;
case 9:
day+=31;
case 8:
day+=31;
case 7:
day+=30;
case 6:
day+=31;
case 5:
day+=30;
case 4:
day+=31;
case 3:
day+=28;
case 2:
day+=31;
case 1:
day=day;break;
default:
printf("sorry,please input error!\n");
}
if(month>=1 && month<=12){
if((year%400==0 || year%4==0 &&year%100!=0) && month>2)
leap=1;
else
leap=0;
if(leap=1 && month>2)
day++;
printf("\nday=%d\n",day);
}
else
printf("\nbyebye\n");
}
----------------解决方案--------------------------------------------------------
还是有问题啊。。。
----------------解决方案--------------------------------------------------------
没事了 我习惯每次输入加逗号了 呵呵
----------------解决方案--------------------------------------------------------
/* 我写的使用数组的 */
#include<stdio.h>
int main(void)
{
int year, month, day, ndays, i;
int ds[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
printf("Please input year, month, day: ");
while(scanf("%d %d %d", &year, &month, &day) == 3)
{
while(getchar() != '\n');
if(year % 4 == 0)
ds[1] == 29;
else
ds[2] == 28;
if(year < 0 || month < 1 || month > 12 || day > ds[month - 1])
{
printf("Error!\n\nPlease input year,month,day: ");
continue;
}
for(i = ndays = 0; i < month - 1; i++)
ndays += ds[i];
ndays += day;
printf(" : %d\n\n", ndays);
printf("Please input year, month, day: ");
}
puts("Done.");
return 0;
}
----------------解决方案--------------------------------------------------------
楼主你的程序没有对输入的数据进行验证!
----------------解决方案--------------------------------------------------------
#include<stdio.h>
int main()
{
int year,month,day;
int leap;
printf("please input year,month,day:");
scanf("%d%d%d",&year,&month,&day);
switch(month)
{
case 12:
day+=30;
case 11:
day+=31;
case 10:
day+=30;
case 9:
day+=31;
case 8:
day+=31;
case 7:
day+=30;
case 6:
day+=31;
case 5:
day+=30;
case 4:
day+=31;
case 3:
day+=28;
case 2:
day+=31;
case 1:
day=day;break;
default:
printf("please input error.\n");
break;
}
if(year%400==0 || year%4==0 &&year%100!=0)
leap=1;
else
leap=0;
if(leap=1 && (month>2&&month<=12))
{day++;
printf("\nday=%d\n",day);}
return 0;
}
关于月份中的天数你还需要限制。
----------------解决方案--------------------------------------------------------
帮你看哈
我改了一下,应该可以达到你的目的了:#include<stdio.h>
void main()
{
int year,month,day;
int leap;
printf("please input year,month,day:");
scanf("%d%d%d",&year,&month,&day);
int i=1;
switch(month)
{
case 12:
day+=30;
printf("day=%d %d \n",day,i++);
case 11:
day+=31;
printf("day=%d %d \n",day,i++);
case 10:
day+=30;
printf("day=%d %d \n",day,i++);
case 9:
day+=31;
printf("day=%d %d \n",day,i++);
case 8:
day+=31;
printf("day=%d %d \n",day,i++);
case 7:
day+=30;
printf("day=%d %d \n",day,i++);
case 6:
day+=31;
printf("day=%d %d \n",day,i++);
case 5:
day+=30;
printf("day=%d %d \n",day,i++);
case 4:
day+=31;
printf("day=%d %d \n",day,i++);
case 3:
day+=28;
printf("day=%d %d \n",day,i++);
case 2:
day+=31;
printf("day=%d %d \n",day,i++);
case 1:
day=day;
printf("day=%d %d \n",day,i++);break;
default:
printf("please input error:\n");
break;
}
if(year%400==0 || (year%4==0 &&year%100!=0) )
leap=1;
else
leap=0;
if(leap==1 && month>2)
day++;
printf("\nday=%d\n",day);
}
----------------解决方案--------------------------------------------------------