2017-01-24 57 views
1

我想创建一个模拟时间的类。这是我到目前为止。如何创建一个模拟时间的类?

namespace TimeSimulationConsole 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     Time startTime = new Time(); 
     startTime.Day = 1; 
     startTime.Month = 1; 
     startTime.Year = 2000; 

     DateTime gameDate = DateTime.Parse(startTime.Day, startTime.Month, startTime.Year); 
     Console.WriteLine(gameDate); 

     Console.ReadLine(); 

    } 
} 

class Time 
{ 
    public int Day { get; set; } 
    public int Month { get; set; } 
    public int Year { get; set; } 
} 
} 

我基本上想要定义一个开始时间,以便我稍后可以修改或添加几天。但现在我只想将其转换为DateTime并通过控制台显示。

我写的代码不起作用,看来我无法解析startTime。

+2

['DateTime.Parse'](https://msdn.microsoft.com/en-us/library/system.datetime.parse.aspx)是用于从一个字符串解析日期时间。你想要的是[DateTime构造函数]之一(https://msdn.microsoft.com/en-us/library/xcfzdy4x.aspx)。 – Blorgbeard

+0

谢谢。好的,我需要再读一遍。即使在观看微软课程后,其中一些内容仍然没有保留。所有这些提醒都非常复杂。 – Dennis

回答

1
class Program 
{ 
    static void Main(string[] args) 
    { 
     Time startTime = new Time(); 
     startTime.Day = 1; 
     startTime.Month = 1; 
     startTime.Year = 2000; 

     DateTime gameDate = new DateTime(startTime.Year, startTime.Month, startTime.Day); 
     Console.WriteLine(gameDate); 

     Console.ReadLine(); 
    } 
} 

class Time 
{ 
    public int Day { get; set; } 
    public int Month { get; set; } 
    public int Year { get; set; } 
} 
+0

太棒了!非常感谢。我想我需要回到关于DateTime的课程......仍然无法提醒所有这些东西。看起来像看一次课程是不够的。 – Dennis

相关问题