第一个C# 程序:
万年历:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace celender
{
class Program
{
public static void Main(string[] args)
{
string ans;
do
{
Console.WriteLine("**************欢迎使用万年历*******************");
Console.WriteLine();
Console.Write("请输入年份:");
int year = int.Parse(Console.ReadLine());
while (year < 1900)
{
Console.WriteLine("输入错误,/n 请重输:");
year = int.Parse(Console.ReadLine());
}
Console.Write("请输入月份:");
int month = int.Parse(Console.ReadLine());
while (month < 1 || month > 12)
{
Console.WriteLine("输入错误,/n 请重输:");
month = int.Parse(Console.ReadLine());
}
Console.WriteLine();
Calc(year, month);
Console.Write("是否继续?(yes/no)");
ans = Console.ReadLine();
}
while (ans == "yes");
}
private static void Calc(int year, int month)
{
int yuefen = 0;
int sumday = 0;
int summonth = 0;
int sumkg = 0;
for (int i = 1900; i < year; i++) // 计算年之间的天数
{
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
{
sumday += 366;
}
else
{
sumday += 365;
}
}
switch (month) // 判断月天数
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
yuefen = 31;
Console.WriteLine("这个月共有{0}", yuefen);
break;
case 2:
{
if ((year % 4 == 0 && year % 100 != 0 )|| (year % 400 == 0))
{
yuefen = 29;
Console.WriteLine("这个月共有{0}", yuefen);
}
else
{
yuefen = 28;
Console.WriteLine("这个月共有{0}", yuefen);
}
}
break;
default:
{
yuefen = 30;
Console.WriteLine("这个月共有{0}", yuefen);
}
break;
}
for (int i = 1; i < month; i++) // 判断输入的月份
{
switch (i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
summonth += 31;
break;
case 2:
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
summonth += 29;
}
else
{
summonth += 28;
}
}
break;
default:
summonth += 30;
break;
}
}
Console.WriteLine();
Console.WriteLine("总天数{0}", sumday);
Console.WriteLine("/n/t/t" + year + " 年 " + month + "月");
sumkg = ((sumday+summonth) % 7) + 1;
Console.WriteLine("星期日/t星期一/t星期二/t星期三/t星期四/t星期五/t星期六");
if (sumkg % 7 != 0) //用于判断第一行是否全为空格,如果全为空格就不再换行
{
for (int j = 1; j <= sumkg; j++) //判断要输出的空格数并且输出应有的空格数
{
Console.Write("/t");
}
for (int k = 1; k <= yuefen; k++) //输出用户输入的这一月的天数
{
Console.Write(" " + k + "/t");
if ((sumkg + k) % 7 == 0) //用于控制每行只能输出七个数字
{
Console.WriteLine();
}
}
}
else
{
for (int k = 1; k <= yuefen; k++) //输出用户输入的这一月的天数
{
Console.Write(" " + k + "/t");
if ((sumkg + k) % 7 == 0) //用于控制每行只能输出七个数字
{
Console.WriteLine();
}
}
}
Console.WriteLine();
Console.WriteLine();
}
}
}