double d = double.Parse(Console.ReadLine());
d = Math.PI; //圆周率
d = Math.Sqrt(d); //开方
d = Math.Ceiling(d); //当为整数时取整,当小数点后大于0,取上限加1取整
d = Math.Floor(d); //舍掉小数点后所有,并取整,不进位
d = Math.Round(d); //四舍五入,当整数部分为奇数时.5进位,当整数部分为偶数时.5舍去
string s = Console.ReadLine();
DateTime dt = new DateTime(); //初始化默认时间为0001年1月1日 12:00:00 am
dt = DateTime.Now; //获取电脑上的本地的时间
int i=dt.Year; //提取年份
DayOfWeek d = dt.DayOfWeek; //获取此实例所表示的日期是星期几
int i = dt.DayOfYear; //获取日期是当年的第几天,只返回int类型值
dt = DateTime.Parse(s);
s = dt.ToString("yyyy年MM月dd日"); //将日期时间类型转换为字符串类型//yyyy,MM,dd,是代位符
System.TimeSpan time = new TimeSpan(); //TimeSpan时间间隔类型表示一个时间间隔,最多加到天
dt=dt.Add(time); //参数是TimeSpan类型的值
dt = dt.AddDays(); //加多少天,参数为double
dt = dt.AddHours(); //加多少个小时,参数为double
dt = dt.AddMonths(); //加多少个月,参数为int,当前面为31天,超出后面的天数的时候,取最大,不进入下一个月
dt = dt.AddYears(); //加多少年,参数为int,当遇到闰年29号,加完不是闰年显示28号,不进入下一个月
练习:
练习2:
//判断年月日是否输入正确
Console.Write("请输入年:");
int nian = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入月:");
int yue = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入日:");
int ri = Convert.ToInt32(Console.ReadLine());
if (nian >= 0 && nian <= 9999)
{
Console.Write("年输入正确!");
}
else
{
Console.Write("年输入错误!");
}
if (yue >= 1 && yue <= 12)
{
Console.Write("月输入正确!");
}
else
{
Console.Write("月输入错误!");
}
if (ri >= 1 && ri <= 31 && (yue == 1 || yue == 3 || yue == 5 || yue == 7 || yue == 8 || yue == 10 || yue == 12))
{
Console.Write("日输入正确!");
}
else if (ri >= 1 && ri <= 30 && (yue == 4 || yue == 6 || yue == 9 || yue == 11))
{
Console.Write("日输入正确!");
}
else if (ri >= 1 && ri <= 29 && nian % 100 == 0&&nian%400==0&& yue == 2)
{
Console.Write("日输入正确!");
}
else if (ri >= 1 && ri <= 29 && nian % 4 == 0 &&nian%100!=0&&nian%400!=0 && yue == 2)
{
Console.Write("日输入正确!");
}
else if (ri >= 1 && ri <= 28 && yue == 2)
{
Console.Write("日输入正确!");
}
else
Console.Write("日输入错误!");
Console.ReadLine();
结果如图:
方法2:
DateTime dt1 = new DateTime();
DateTime dt2 = new DateTime();
int i = 0;
int sum = 0;
try
{
dt1 = DateTime.Parse(Console.ReadLine());
dt2 = DateTime.Parse(Console.ReadLine());
}
catch (Exception e)
{
Console.Write("输入错误!");
i++;
}
finally
{
if(i==0)
{
if(dt1>dt2)
{
DateTime dt3=dt2;
dt2=dt1;
dt1=dt3;
}
for(int k=dt1.Year;k<dt2.Year;k++)
{
if(k%100==0)
{
if(k%400==0)
{
Console.Write(k+"是闰年");
sum+=366;
}
else
{
Console.Write(k+"不是闰年");
sum+=365;
}
}
}
}
}
}
sum+=dt2.DayOfYear-dt1.DayOfYear;
Console.WriteLine("相差"+sum+"天");
Console.ReadLine();
练习3:
//随机生成4位验证码,并输入比对是否输入正确
string s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random r = new Random();
int yi = r.Next(63);
int er = r.Next(63);
int san = r.Next(63);
int si = r.Next(63);
string a = s.Substring(yi, 1);
string b = s.Substring(er,1);
string c = s.Substring(san,1);
string d = s.Substring(si,1);
string suiji = a + b + c + d;
Console.WriteLine(suiji);
Console.Write("请输入:");
while (true)
{
string shuru = Console.ReadLine();
shuru = shuru.ToUpper();
suiji = suiji.ToUpper();
if (shuru == suiji)
{
Console.Write("输入正确!");
break;
}
else
{
Console.Write("请重新输入!");
}
}
Console.ReadLine();
练习4: