带你学c带你飞数组动动题,生命计算器V2.0,结果不一致,麻烦帮忙改改代码
#include<stdio.h>int main()
{
int count = 0,count1 = 0;
int i,year1,mouth1,day1,year2,mouth2,day2;
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for(i = 0;i<1;i++)
{
printf("请输入你的生日:");
scanf("%d-%d-%d",&year1,&mouth1,&day1);
printf("请输入今年日期:");
scanf("%d-%d-%d",&year2,&mouth2,&day2);
//printf("%d%d%d今年%d%d%d",year1,mouth1,day1,year2,mouth2,day2);//调试专用
while(year1 <= year2)
{
days = (year1 % 400 == 0 || (year1 % 4 == 0 && year1 % 100 != 0)) ? 29 : 28;
while(mouth1 <= 12)
{
while (day1 <= days)
{
if (year1 == year2 && mouth1 == mouth2 && day1 == day2)
{
goto FINISH1; // 跳出多层循环才被迫用goto语句
}
day1++;
count1++;
}
day1 = 1;
mouth1++;
}
mouth1 = 1;
year1++;
}
}
FINISH1: printf("你在这个世界上总共生存了%d天\n", count1);
while(year1 <= (year1+80))
{
days = (year1 % 400 == 0 || (year1 % 4 == 0 && year1 % 100 != 0)) ? 29 : 28;
while(mouth1 <= 12)
{
while (day1 <= days)
{
if (year1 == year2 && mouth1 == mouth2 && day1 == day2)
{
goto FINISH2; // 跳出多层循环才被迫用goto语句
}
day1++;
count++;
}
day1 = 1;
mouth1++;
}
mouth1 = 1;
year1++;
}
FINISH2: printf("如果能活到80岁,你还剩下%d天\n", count-count1);
return 0;
}2,0 请输入你的生日:1988-05-20
请输入今年日期:2016-03-28
你在这个世界上总共生存了10174天
如果能活到80岁,你还剩下19046天 29220 10174
马虎了呀,错误就在你的判断离80岁还剩多少天那里
if (year1 == year2 && mouth1 == mouth2 && day1 == day2)不能再用 year2 了
#include<stdio.h>
int main()
{
int count = 0,count1 = 0;
int i,year1,mouth1,day1,year2,mouth2,day2,temp;
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for(i = 0;i<1;i++)
{
printf("请输入你的生日:");
scanf("%d-%d-%d",&year1,&mouth1,&day1);
printf("请输入今年日期:");
scanf("%d-%d-%d",&year2,&mouth2,&day2);
//printf("%d%d%d今年%d%d%d",year1,mouth1,day1,year2,mouth2,day2);//调试专用
while(year1 <= year2)
{
days = (year1 % 400 == 0 || (year1 % 4 == 0 && year1 % 100 != 0)) ? 29 : 28;
while(mouth1 <= 12)
{
while (day1 <= days)
{
if (year1 == year2 && mouth1 == mouth2 && day1 == day2)
{
goto FINISH1; // 跳出多层循环才被迫用goto语句
}
day1++;
count1++;
}
day1 = 1;
mouth1++;
}
mouth1 = 1;
year1++;
}
}
FINISH1: printf("你在这个世界上总共生存了%d天\n", count1);
temp = year1+80;
while(year1 <= temp)
{
days = (year1 % 400 == 0 || (year1 % 4 == 0 && year1 % 100 != 0)) ? 29 : 28;
while(mouth1 <= 12)
{
while (day1 <= days)
{
if (year1 == temp && mouth1 == mouth2 && day1 == day2)
{
goto FINISH2; // 跳出多层循环才被迫用goto语句
}
day1++;
count++;
}
day1 = 1;
mouth1++;
}
mouth1 = 1;
year1++;
}
FINISH2: printf("如果能活到80岁,你还剩下%d天 %d %d \n", count-count1,count,count1);
return 0;
} 大马强 发表于 2022-1-9 18:35
马虎了呀,错误就在你的判断离80岁还剩多少天那里
if (year1 == year2 && mouth1 == mouth2 && day1 ==...
我多用了一个临时变量
因为year1是改变的,而你year1+80一样会改变,要注意下 大马强 发表于 2022-1-9 18:36
我多用了一个临时变量
因为year1是改变的,而你year1+80一样会改变,要注意下
非常谢谢老师的提醒,弟子受教了,谢谢{:5_109:} 爱学习的阿瑶 发表于 2022-1-9 21:06
非常谢谢老师的提醒,弟子受教了,谢谢
{:10_254:}
页:
[1]