c# 输入年份输出一年的日历

YQ.yang 2020-03-26 01:16:06
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Day01
{
class Program
{
static void Main(string[] args)
{

Console.WriteLine("请输入年份");
int year = int.Parse(Console.ReadLine());
bool Estimate=LeapYear(year);//判断是否闰年
for(int month = 1; month < 13; month++)
{
int j;
Console.WriteLine("______________________________________________________");
Console.WriteLine("\n");
Console.WriteLine("\t\t\t"+ month+"月");
Console.WriteLine("\n");
int day = DayOfMonth(Estimate, month);//判断月份的天数
int blank = GetWeekByDay(year, month, day);//计算空格
Console.Write("日\t一\t二\t三\t四\t五\t六\n");
for ( j = 1; j <= blank; j++)//添加空格
{
Console.Write("\t");
}
for(int i=1;i<=day;i++)
{
int a = 8 - j;
if ((i==a)||(i==(a+7))|| (i == (a + 14)) || (i == (a + 21)) || (i == (a + 28)) || (i == (a + 35)) || (i == day)) Console.Write(i +"\n");
else Console.Write(i + "\t");
}
Console.WriteLine( "\n\n");

}
Console.ReadLine();
}
private static int DayOfMonth(bool Estimate, int month)//判断指定月份的天数
{
int day;
if (Estimate == true)
{
if(month==1||month==3||month==5||month==7||month==8||month==12) day = 31;
else if (month==4||month==6||month==9||month==10||month==11) day=30 ;
else day=29;
}
else
{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 12) day = 31;
else if (month == 4 || month == 6 || month == 9 || month == 10 || month == 11) day = 30;
else day = 28;
}
return day;
}


private static bool LeapYear(int year)//判断是否为闰年
{
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
return true;
else
return false;
}
private static int GetWeekByDay(int year, int month, int day)//根据年月日计算星期数
{
DateTime dt = new DateTime(year, month, day);
return (int)dt.DayOfWeek;
}


}
}
...全文
554 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
milaoshu1020 2020-03-27
  • 打赏
  • 举报
回复
引用 7 楼 weixin_41009655 的回复:
好用了!感谢大佬!
不客气,我也新手,共同学习吧;
YQ.yang 2020-03-27
  • 打赏
  • 举报
回复
引用 6 楼 milaoshu1020 的回复:
就像这样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Day01
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("请输入年份");
int year = int.Parse(Console.ReadLine());
bool Estimate=LeapYear(year);//判断是否闰年
for(int month = 1; month <= 12; month++)
{
int j;
Console.WriteLine("______________________________________________________");
Console.WriteLine("\n");
Console.WriteLine("\t\t\t"+ month+"月");
Console.WriteLine("\n");
int day = DayOfMonth(Estimate, month);//判断月份的天数
int blank = GetWeekByDay(year, month, 1);//计算空格
Console.Write("日\t一\t二\t三\t四\t五\t六\n");
for (j=1; j<=blank; j++)//添加空格
{
Console.Write("\t");
}
for(int i=1; i<=day; i++)
{
int a = 8 - j;
if ((i+7-a)%7==0 || i==day) {
Console.Write(i + "\n");
} else {
Console.Write(i + "\t");
}
}
Console.WriteLine( "\n\n");

}
Console.ReadLine();
}
private static int DayOfMonth(bool Estimate, int month)//判断指定月份的天数
{
int day;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 4:
case 6:
case 9:
case 11:
day = 30;
break;
case 2:
if (Estimate)
day = 29;
else
day = 28;
break;
default:
day = 0;
break;
}

return day;
}

//判断是否为闰年
private static bool LeapYear(int year)
{
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
return true;
else
return false;
}

//根据年月日计算星期数
private static int GetWeekByDay(int year, int month, int day)
{
DateTime dt = new DateTime(year, month, day);
return (int)dt.DayOfWeek;
}
}
}
好用了!感谢大佬!
milaoshu1020 2020-03-27
  • 打赏
  • 举报
回复
就像这样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Day01
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("请输入年份");
            int year = int.Parse(Console.ReadLine());
            bool Estimate=LeapYear(year);//判断是否闰年
            for(int month = 1; month <= 12; month++)
            {
                int j;
                Console.WriteLine("______________________________________________________");
                Console.WriteLine("\n");
                Console.WriteLine("\t\t\t"+ month+"月");
                Console.WriteLine("\n");
                int day = DayOfMonth(Estimate, month);//判断月份的天数
                int blank = GetWeekByDay(year, month, 1);//计算空格
                Console.Write("日\t一\t二\t三\t四\t五\t六\n");
                for (j=1; j<=blank; j++)//添加空格
                {
                    Console.Write("\t");
                }         
                for(int i=1; i<=day; i++)
                {
                    int a = 8 - j;
					if ((i+7-a)%7==0 || i==day) {
						Console.Write(i + "\n");
					} else {
						Console.Write(i + "\t");
					}
                } 
                Console.WriteLine( "\n\n");
               
            } 
            Console.ReadLine();
        }
        private static int DayOfMonth(bool Estimate, int month)//判断指定月份的天数
        {
            int day;
			switch (month)
			{
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				day = 31;
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				day = 30;
				break;
			case 2:
				if (Estimate)
					day = 29;
				else
					day = 28;
				break;
			default:
				day = 0;
				break;
			}

            return day;
        }
        
		//判断是否为闰年
		private static bool LeapYear(int year)
		{
			if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
				return true;
			else
				return false;
	    }
        
		//根据年月日计算星期数
		private static int GetWeekByDay(int year, int month, int day)
		{
			DateTime dt = new DateTime(year, month, day);
			return (int)dt.DayOfWeek; 
		}
	}
}
milaoshu1020 2020-03-27
  • 打赏
  • 举报
回复
引用 4 楼 weixin_41009655 的回复:
是的!程序只考虑的平闰年······我对日历不太了解啊!
不是平闰年的问题; int blank = GetWeekByDay(year, month, day);//计算空格 改成: int blank = GetWeekByDay(year, month, 1);//计算空格 就对了;
weixin_57928639 2022-03-21
  • 举报
回复
@milaoshu1020 为什么这么改啊
YQ.yang 2020-03-26
  • 打赏
  • 举报
回复
引用 3 楼 milaoshu1020 的回复:
楼主,你的日历好像和实际的日期不一样啊;
是的!程序只考虑的平闰年······我对日历不太了解啊!
milaoshu1020 2020-03-26
  • 打赏
  • 举报
回复
楼主,你的日历好像和实际的日期不一样啊;
YQ.yang 2020-03-26
  • 打赏
  • 举报
回复
引用 1 楼 大西瓜一块五一斤♏ 的回复:
分享?
第一次用发错地方了orz
  • 打赏
  • 举报
回复
分享?

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧