namespace test5 { class Program { static void Main(string[] args) { Console.WriteLine("请输入生日日期:(例如:2000-01-01或2000/01/01)"); int year=int.Parse (Console.ReadLine()); int month=int.Parse (Console .ReadLine()); int day=int.Parse(Console.ReadLine ()); DateTime birthday=new DateTime(year,month,day); Console.WriteLine("你的年龄是:{0}周岁,GetAge(birthday)"); } public static int GetAge(DateTime birthday); { int x=DateTime.Today.Year ; int y=DateTime.Today.Month; int z=DateTime.Today.Day ; if(z<birthday.day) y--; if(y<birthday.month) x--; return y-birthday.year; {
namespace test5 { class Program { static void Main(string[] args) { Console.WriteLine("请输入生日日期:(例如:2000-01-01或2000/01/01)"); int year=int.Parse (Console.ReadLine()); int month=int.Parse (Console .ReadLine()); int day=int.Parse(Console.ReadLine ()); DateTime birthday=new DateTime(year,month,day); Console.WriteLine("你的年龄是:{0}周岁,GetAge(birthday)"); } public static int GetAge(DateTime birthday); { var days=(DateTime.Today-birthday).Days; int age = 0; if (days%365==0) { age = days/ 365; } else { age = days/ 365+1; } return age; } {
随手打的,忘记你定义的是int类型了,先转换下类型在计算
namespace ConsoleApplication3 { class Program { static void Main(string[] args) { Console.WriteLine("请输入生日日期:(例如:2000-01-01或2000/01/01)"); int year=int.Parse (Console.ReadLine()); int month=int.Parse (Console .ReadLine()); int day=int.Parse(Console.ReadLine ()); DateTime birthday=new DateTime(year,month,day); Console.WriteLine(string.Format("你的年龄是:{0}周岁",GetAge(birthday))); } public static int GetAge(DateTime birthday) { int x = DateTime.Today.Year; int y = DateTime.Today.Month; int z = DateTime.Today.Day; double monthDay = birthday.Month + (double)birthday.Day / 100; double monthDayNow = DateTime.Today.Month + (double)DateTime.Today.Day / 100; if (monthDay > monthDayNow) return x - birthday.Year-1; else return x - birthday.Year; } } }
public static int GetAge(DateTime dt) { int a = DateTime.Now.Year - dt.Year; int runcount = 0; int pingcount = 0; for (int i = 1; i <= a; i++) { if (DateTime.IsLeapYear(dt.Year+i)) { runcount++; } else { pingcount++; } } return runcount + pingcount + 1; }
例子中会生成一个长度为200的字符串,内容就是前100个asscii码字符分别加一空格
"...A B C D E ...a b c ..."这个样子的
sb1.Append(" ");//这一行是在字符串结尾加上一个空格
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入生日日期:(例如:2000-01-01或2000/01/01)");
int year=int.Parse (Console.ReadLine());
int month=int.Parse (Console .ReadLine());
int day=int.Parse(Console.ReadLine ());
DateTime birthday=new DateTime(year,month,day);
Console.WriteLine("你的年龄是:{0}周岁,GetAge(birthday)");
}
public static int GetAge(DateTime birthday);
{
int x=DateTime.Today.Year ;
int y=DateTime.Today.Month;
int z=DateTime.Today.Day ;
if(z<birthday.day)
y--;
if(y<birthday.month)
x--;
return y-birthday.year;
{
可是这里 if(z<birthday.day)
y--;
if(y<birthday.month)
x--;
return y-birthday.year;
{
总是报错
你提的这个问题,其实本不该提,提了就说明了你学习方法不对。
sb1.Append("")是什么意思?你打开visual studio ,输入这些代码,然后按F12,文档帮助会告诉你,这个方法是什么意思。==========
最新文章:浅谈C#数组(一) https://mp.weixin.qq.com/s/76LPgHgCYs8FPX3dAl9w6w
欢迎关注微信公众号 “产品技术知与行” ,解读技术经典书籍(C#,Java,Js),发表技术专题、提供源码下载,打造全面结构化知识库,欢迎对全栈/跨语言技术有兴趣的小伙伴关注。
可是这里 if(z<birthday.day)
y--;
if(y<birthday.month)
x--;
return y-birthday.year;
{
总是报错
日期计算哪有那么简单,如果生日是1月怎么减?你得好好看看算法。
c#有做日期计算的方法
var asas=(DateTime.Today-new DateTime(2017,3,19)).Days;
int age = 0;
if (asas%365==0)
{
age = asas / 365;
}
else
{
age = asas / 365+1;
}
可是这里 if(z<birthday.day)
y--;
if(y<birthday.month)
x--;
return y-birthday.year;
{
总是报错
日期计算哪有那么简单,如果生日是1月怎么减?你得好好看看算法。
c#有做日期计算的方法
那我的程序要怎么改啊
double monthDay = birthday.month+day/100;
double monthDayNow = DateTime.today.month+DateTime.today.day/100;
if(monthDay >=monthDayNow )
return y-birthday.year;
else
return y-birthday.year-1;
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入生日日期:(例如:2000-01-01或2000/01/01)");
int year=int.Parse (Console.ReadLine());
int month=int.Parse (Console .ReadLine());
int day=int.Parse(Console.ReadLine ());
DateTime birthday=new DateTime(year,month,day);
Console.WriteLine("你的年龄是:{0}周岁,GetAge(birthday)");
}
public static int GetAge(DateTime birthday);
{
var days=(DateTime.Today-birthday).Days;
int age = 0;
if (days%365==0)
{
age = days/ 365;
}
else
{
age = days/ 365+1;
}
return age;
}
{
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入生日日期:(例如:2000-01-01或2000/01/01)");
int year=int.Parse (Console.ReadLine());
int month=int.Parse (Console .ReadLine());
int day=int.Parse(Console.ReadLine ());
DateTime birthday=new DateTime(year,month,day);
Console.WriteLine(string.Format("你的年龄是:{0}周岁",GetAge(birthday)));
}
public static int GetAge(DateTime birthday)
{
int x = DateTime.Today.Year;
int y = DateTime.Today.Month;
int z = DateTime.Today.Day;
double monthDay = birthday.Month + (double)birthday.Day / 100;
double monthDayNow = DateTime.Today.Month + (double)DateTime.Today.Day / 100;
if (monthDay > monthDayNow)
return x - birthday.Year-1;
else
return x - birthday.Year;
}
}
}
int a = DateTime.Now.Year - dt.Year;
int runcount = 0;
int pingcount = 0;
for (int i = 1; i <= a; i++)
{
if (DateTime.IsLeapYear(dt.Year+i))
{
runcount++;
}
else
{
pingcount++;
}
}
return runcount + pingcount + 1;
}
我还想问问showmessage怎么用
另外这些方法直接在vs里f12,就可以找到使用说明的。
写程序要会自己琢磨,查资料。
public static int GetAge(DateTime birthday);
{
int x=DateTime.Today.Year ;
int y=DateTime.Today.Month;
int z=DateTime.Today.Day ;
if(z<birthday.day)
y--;
if(y<birthday.month)
x--;
return y-birthday.year;
{
首先是大括号,为啥是一个方向?
然后按照你这个逻辑如果当前天数比生日天数少,就把当前月份-1;如果当前月份比生日月份小就把当前年份-1,然后现在也不知道x y有没有都-过1,最后用当前月份-生日年份,请问这样的逻辑能走得通吗?
然后我贴一下我的代码,可供参考,没有做容错处理,我就不分析了:DateTime now = DateTime.Now.Date;
int year = now.Year - birthday.Year;
if(now.Month<birthday.Month)
{
year--;
}
else if(now.Month==birthday.Month&& now.Day<birthday.Day)
{
year--;
}
return year;