方法一:使用if……else语句 

#include"stdio.h"
#include"conio.h"
int main()
{
	int year,month,days;

	printf("请依次输入整数的某年某月:");

	scanf("%d%d",&year,&month);

	if(month>12||month<1)                       //判断输入的月份数据是否正确

	printf("输入的月份数据错误\n");

	else
	{

	if((year%4==0&&year%100!=0)||(year%400==0)) //闰年的判断

	{
		if(month==2)
			days = 29;

		else if(month==4||month==6||month==9||month==11)

			days = 30;
		
		else

			days = 31;

		}

	else

	{
	
		if(month==2)

			days = 28;

		else if(month==4||month==6||month==9||month==11)

			days = 30;
		
		else

			days = 31;

		}
	
	}

	printf("the month of this year has these days: %d\n",days);

	getch();

	return 0;
}

方法二:使用switch语句

#include"stdio.h"
#include"conio.h"
int runnian(int year);//自定义求润年函数

void main()

{

	int year,month,days;

	printf("请依次输入整数的某年某月:");

	scanf("%d%d",&year,&month);

	if(month>12||month<1)

	printf("输入的月份数据错误\n");

	else
	{

	int days = 31;
	switch(month)
	{
		case 4:
		case 6:
		case 9:
		case 11:
		{
			days = 30;
			break;
		}

		case 2:

		{
			if(runnian(year))

				days = 29;
			else

				days = 28;

			break;

		}
	}

	printf("该年该月有: %d 天\n",days);

	}

	getch();

}

int runnian(int year)//自定义求闰年函数

{
	if((year%4==0&&year%100!=0)||(year%400==0))//符合闰年的条件
	{
	
	return 1;

	}

	return 0;
	}



考虑到一些读者需要相关资料和解决一些疑问,因此我新建立了一个学习交流群,我在群文件里上传了一些资料,需要的读者可以入群下载。

群中文件资料我会时常更新,主要资料是51单片机开发、32单片机开发、编程、嵌入式学习、算法控制等。

由于群中文件还在不断更新上传,有些方面的资料还不太完善,希望大家理解。若群中涉及违规行为,欢迎举报!

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐