首页 > 代码库 > C#的第一个应用

C#的第一个应用

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

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
while (true)
{
{
List<string> str = new List<string>();
int year, month;
#region 获取正确的年份
while (true)
{
Console.WriteLine("请输入正确的年份(1900-2100):");
year = int.Parse(Console.ReadLine());
if (year < 1900 || year > 2100)
Console.WriteLine("输入错误,请重新输入年份");
else
{
while (true)
{
Console.WriteLine("请输入月份(1-12):");
month = int.Parse(Console.ReadLine());
if (month < 1 || month > 12)
Console.WriteLine("输入错误,请输入正确的月份");
else
{
break;
}

}
break;
}
Console.ReadLine();
Console.Clear();

}
#endregion
#region 计算到那年年的天数
int dayofyear, crossyear = 0;
for (int i = 1900; i < year; i++)
{
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
dayofyear = 366;
else
dayofyear = 365;
crossyear += dayofyear;

}
#endregion
#region 到那月的天数
int dayofmonth, crossmonth = 0;
for (int i = 1; i < month; i++)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
if (i == 2)
dayofmonth = 29;
else if (i < 8 && i % 2 != 0 || i >= 8 && i % 2 == 0)
dayofmonth = 31;
else
dayofmonth = 30;
}
else
{
if (i == 2)
dayofmonth = 28;
else if (i < 8 && i % 2 != 0 || i >= 8 && i % 2 == 0)
dayofmonth = 31;
else
dayofmonth = 30;
}
crossmonth += dayofmonth;
}
#endregion
#region 计算空格
int week, space;
week = (crossyear + crossmonth) % 7 + 1;
space = week - 1;
for (int i = 0; i < space; i++)
str.Add("");
#endregion
#region 添加台历
int day;
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
if (month == 2)
day = 29;
else if (month < 8 && month % 2 != 0 || month >= 8 && month % 2 == 0)
day = 31;
else
day = 30;
}
else
{
if (month == 2)
day = 28;
else if ((month < 8 && month % 2 != 0) || (month >= 8 && month % 2 == 0))
day = 31;
else
day = 30;
}
for (int i = 1; i <= day; i++)
str.Add(i.ToString());
#endregion
#region 输出台历
Console.WriteLine("******************************************************");
Console.WriteLine("一\t二\t三\t四\t五\t六\t日");
for (int i = 0; i < str.Count; i++)
{
if (i % 7 == 0 && i != 0)
Console.WriteLine();

Console.Write(str[i] + "\t");
}
Console.WriteLine();
Console.WriteLine("******************************************************");
#endregion
}
Console.WriteLine("程序结束,请按确认键结束");
Console.ReadLine();
Console.Clear();

}
}
}
}

 

C#的第一个应用