C语言元组课后作业,打印每月的天数
我最初打的代码,不知道哪里错了{:5_100:} #include <stdio.h>int main()
{
int month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int i = 0;
int year;
printf("请输入一个年份:");
scanf("%d\n",&year);
while (year % 4 == 0){
if (year % 100 != 0){
month = 29;
}
else if (year % 400 == 0){
month = 29;
}
break;
}
for(i = 0; i < 12; i++){
printf("%d月份 : %d天\n", i+1, month);
}
return 0;
}
1. scanf() 不要用 \n
2. 没必要用 while,用 if 就可以了,而且把 break 去掉
3. 24 行 month[ i ] 打成了 month
代码帮你改好了:
#include <stdio.h>
int main()
{
int month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int i = 0;
int year;
printf("请输入一个年份:");
scanf("%d", &year);
if (year % 4 == 0)
{
if (year % 100 != 0)
{
month = 29;
}
else if (year % 400 == 0)
{
month = 29;
}
}
for (i = 0; i < 12; i++)
{
printf("%d月份 : %d天\n", i + 1, month);
}
return 0;
}
本帖最后由 baige 于 2020-7-30 17:37 编辑
#include <stdio.h>
int main()
{
int month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int i = 0;
int year;
printf("请输入一个年份:");
scanf("%d",&year);
if((year%4==0&&year%100!=0)||year%400==0)
month = 29;
for(i = 0; i < 12; i++){
printf("%d月份 : %d天\n", i+1, month);
}
return 0;
}
页:
[1]