c++编程输入某年某月某日,判断这一天是这一年的第几天.
我自己写的#include<iostream>usingnamespacestd;voidmain(){inta,b,c,d;cout<<"请输入年,月,日"<<endl...
我自己写的
#include<iostream>
using namespace std;
void main()
{ int a,b,c,d;
cout<<"请输入年,月,日"<<endl;
cin>>a>>b>>c;
d=0;
switch(b)
{
case12:d=d+c+30;
case11:d=d+c+31;
case10:d=d+c+30;
case9:d=d+c+31;
case8:d=d+c+31;
case7:d=d+c+30;
case6:d=d+c+31;
case5:d=d+c+30;
case4:d=d+c+30;
case3:
if((a%4==0&&a%100!=0)||(a%400==0))
d=d+c+29;
else
d=d+c+28;
case2:d=d+c+31;
case1:d=d+c;
}
cout<<"为这一年的第"<<d<<"天"<<endl;
}
为什么最后解决方案出来的总是d=0? 展开
#include<iostream>
using namespace std;
void main()
{ int a,b,c,d;
cout<<"请输入年,月,日"<<endl;
cin>>a>>b>>c;
d=0;
switch(b)
{
case12:d=d+c+30;
case11:d=d+c+31;
case10:d=d+c+30;
case9:d=d+c+31;
case8:d=d+c+31;
case7:d=d+c+30;
case6:d=d+c+31;
case5:d=d+c+30;
case4:d=d+c+30;
case3:
if((a%4==0&&a%100!=0)||(a%400==0))
d=d+c+29;
else
d=d+c+28;
case2:d=d+c+31;
case1:d=d+c;
}
cout<<"为这一年的第"<<d<<"天"<<endl;
}
为什么最后解决方案出来的总是d=0? 展开
4个回答
展开全部
#include <iostream>
using namespace std;
int main()
{
int day=0; // 日期
int month=0; // 月份
int year=0; // 年份
int sum=0; // 一年中的第几天
cin>>year>>month>>day;
//write your own codes
int m=0;
if(year%4==0&&year%100!=0||year%400==0)m=29;//闰年二月份29天
else m=28;
switch (month)
{
case 12:sum+=30;
case 11:sum+=31;
case 10:sum+=30;
case 9:sum+=31; //如果是某个月,则加上前一个月份的所有天数;
case 8:sum+=31;
case 7:sum+=30;
case 6:sum+=31;
case 5:sum+=30;
case 4:sum+=31;
case 3:sum+=m;
case 2:sum+=31;
case 1:sum+=day; //最后加上这个月的天数;
}
cout<<sum;
return 0;
}
//也可以在每条case语句里直接算出天数,不过要记得加上break语句
展开全部
关键字case后面留个空格哈:
#include<iostream>
using namespace std;
void main()
{
int a, b, c, d;
cout << "请输入年,月,日" << endl;
cin >> a >> b >> c;
d = 0;
switch (b)
{
case 12:d = d + c + 30;
case 11:d = d + c + 31;
case 10:d = d + c + 30;
case 9:d = d + c + 31;
case 8:d = d + c + 31;
case 7:d = d + c + 30;
case 6:d = d + c + 31;
case 5:d = d + c + 30;
case 4:d = d + c + 30;
case 3:
if ((a % 4 == 0 && a % 100 != 0) || (a % 400 == 0))
d = d + c + 29;
else
d = d + c + 28;
case 2:d = d + c + 31;
case 1:d = d + c;
}
cout << "为这一年的第" << d << "天" << endl;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你没敲错我这个就没毛病哦 我运行过的
运行环境 gcc vc6.0
#include <iostream>
using namespace std;
main()
{
cout<<"请输入年、月、日"<<endl;
int year,month,day,sum=0;//定义年月日及天数
cin>>year;
cin>>month;
cin>>day;//输入
switch (month)
{
case 1:
sum=day;break;
case 2:
sum=day+31;
break;
case 3:
sum=day+59;
break;
case 4:
sum=day+90;
break;
case 5:
sum=day+120;
break;
case 6:
sum=day+151;
break;
case 7:
sum=day+181;
break;
case 8:
sum=day+212;
break;
case 9:
sum=day+243;
break;
case 10:
sum=day+273;
break;
case 11:
sum=day+304;
break;
case 12:
sum=day+334;
break;
}
if(((year%4==0&&year%100!=0)||year%400==0)&&month>=3)
{
sum=sum+1;
}//如果是闰年,那么三月及以后月份计算时+1,(非闰年2月28天,闰年29天)
cout<<sum<<endl;
}
运行环境 gcc vc6.0
#include <iostream>
using namespace std;
main()
{
cout<<"请输入年、月、日"<<endl;
int year,month,day,sum=0;//定义年月日及天数
cin>>year;
cin>>month;
cin>>day;//输入
switch (month)
{
case 1:
sum=day;break;
case 2:
sum=day+31;
break;
case 3:
sum=day+59;
break;
case 4:
sum=day+90;
break;
case 5:
sum=day+120;
break;
case 6:
sum=day+151;
break;
case 7:
sum=day+181;
break;
case 8:
sum=day+212;
break;
case 9:
sum=day+243;
break;
case 10:
sum=day+273;
break;
case 11:
sum=day+304;
break;
case 12:
sum=day+334;
break;
}
if(((year%4==0&&year%100!=0)||year%400==0)&&month>=3)
{
sum=sum+1;
}//如果是闰年,那么三月及以后月份计算时+1,(非闰年2月28天,闰年29天)
cout<<sum<<endl;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<stdio.h>
#include<stdlib.h>
int main() {
int year, month, day, look = 0, day1 = 0;
printf("请输入日期:");
scanf_s("%d,%d,%d",&year, &month,&day);
if ((year % 400 == 0) || (year % 100 == 0) && (year % 4 != 0))
look = 1;
switch(month)
{
case 2:day1 = 31; break;
case 3:day1 = 59; break;
case 4:day1 = 90; break;
case 5:day1 = 120; break;
case 6:day1 = 151; break;
case 7:day1 = 181; break;
case 8:day1 = 212; break;
case 9:day1 = 243; break;
case 10:day1 = 273; break;
case 11:day1 = 303; break;
case 12:day1 = 334; break;
}
if (look)
day1 += day + 1;
else
day1 += day;
printf("%d年%d月%d日是这一年的第%d天\n", year, month, day, day1);
system("pause");
return 0;
}
#include<stdlib.h>
int main() {
int year, month, day, look = 0, day1 = 0;
printf("请输入日期:");
scanf_s("%d,%d,%d",&year, &month,&day);
if ((year % 400 == 0) || (year % 100 == 0) && (year % 4 != 0))
look = 1;
switch(month)
{
case 2:day1 = 31; break;
case 3:day1 = 59; break;
case 4:day1 = 90; break;
case 5:day1 = 120; break;
case 6:day1 = 151; break;
case 7:day1 = 181; break;
case 8:day1 = 212; break;
case 9:day1 = 243; break;
case 10:day1 = 273; break;
case 11:day1 = 303; break;
case 12:day1 = 334; break;
}
if (look)
day1 += day + 1;
else
day1 += day;
printf("%d年%d月%d日是这一年的第%d天\n", year, month, day, day1);
system("pause");
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询