-1

我正在尝试创建与这些字段对应的属性,但是在不使用 DateTime 类的情况下,我无法制定设置正确日、月和年的逻辑。

因此,用户将一次输入一个数据:按该顺序输入日、月和年。

日期应该在 1 到 30 或 31 或 28 之间 - 基于月份。月份必须介于 1 和 12 之间,年份必须小于或等于 2013

然后我需要制作一个测试用例,它将采用日、月和年的 3 个值并创建一个日期实例。

然后我必须创建一个 Sentinel 控制循环来要求用户输入日、月和年,并使用构造函数创建一个带有用户输入的 Date 实例。如果用户为日、月或年输入 0,则终止程序。

我主要是无法理解逻辑。

public class Date
{
   public int Month { get; set; }

   public int Day { get; set; }
   public int Year { get; set; }

   public Date ( int monthValue, int dayValue, int yearValue)
   {
       Month = monthValue;
       Day = dayValue;
       Year = yearValue;
   }
   public int theMonth
   {
       get {return Month;} 

       set
       {
           if (value <= 12) Month = value;

       }

    }
   public int theDay
   {
       get { return theDay; }
       set
       {
           if (value <= 30) Day = value;
           else if (value == 31) Day = value;
           else if (value == 28) Day = value;
       }
   }


   public override string ToString()
   {
        return String.Format( "{0}/{1}/{2}", Month, Day, Year);
   }
}
4

3 回答 3

1

您可以有一个包含月份 (0-11) 和每个对应天数的数组。对于二月,您放置最常见的一个 (28),但如果为二月输入 29... 您需要检查年份是否为闰年。

要确定某一年是否为闰年,请执行以下步骤:

1.如果年份能被4整除,则转步骤2,否则转步骤5。

2.如果年份能被100整除,则转步骤3,否则转步骤4。

3.如果年份能被400整除,则转步骤4,否则转步骤5。

4.这一年是闰年(有366天)。

5.这一年不是闰年(它有365天)。

上述过程可以概括为:

if(year % 4 == 0 && year % 100 == 0 && year % 400 == 0){
  return true; // it is leap year
}
else{
return false; //it is not leap
}
于 2013-06-14T18:26:37.250 回答
0

您想创建自己的类 myDateTime,它具有整数 Day、Month、Year。

然后你会想要在你的程序开始的地方做一个 while(userinput != 0) 循环。当您收到输入时,您需要验证每个整数,然后,如果数据有效,则使用这些值创建 myDateTime 的实例。

数据验证示例:

if(userInputDay >= 1 && userInputDay <= 31 )
    myDateTime.Day = userInputDay;

从您的代码:

if (value <= 30) Day = value;
else if (value == 31) Day = value;
else if (value == 28) Day = value;

与以下逻辑相同:

if(value <= 31) Day = value;

这是你的一个开始,它非常愚蠢,不做任何错误检查,但它是一个开始

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Input Month, Day, Year separated by spaces");
        string input = Console.ReadLine();

        while (input != "0")
        {
            String[] nums = input.Split(' ');

            int month = Convert.ToInt32(nums[0]);
            int day = Convert.ToInt32(nums[1]);
            int year = Convert.ToInt32(nums[2]);

            if (month > 0 && month <= 12 &&
                day > 0 && day <= 31 &&
                year <= 2013)
            {
                //valid
            }

            Console.WriteLine("Input Month, Day, Year separated by spaces");
            input = Console.ReadLine();
        }

    }


}

class myDateTime
{
    public int Day;
    public int Month;
    public int Year;

    public myDateTime(int m, int d, int y)
    {
        Month = m;
        Day = d;
        Year = y;
    }
}
于 2013-06-14T18:18:47.433 回答
0

So you might want to start with a container which to hold the date components. You had a pretty good start with your Date class, but I would submit you don't want those values mutating once set. One method to accomplish this by using a simple immutable struct:

public struct MyDateTime
{
    private readonly int year;

    private readonly byte month;

    private readonly byte day;

    public MyDateTime(int year, byte month, byte day)
    {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int Year
    {
        get
        {
            return this.year;
        }
    }

    public byte Month
    {
        get
        {
            return this.month;
        }
    }

    public byte Day
    {
        get
        {
            return this.day;
        }
    }
}

Once you have that, you can begin to add validation logic to the constructor to meet your constraints:

public MyDateTime(int year, byte month, byte day)
{
    if ((year < 0) || (year > 2013))
    {
        throw new ArgumentOutOfRangeException("year", year, "year must be greater than or equal to zero and less than 2013.");
    }

    if ((month < 1) || (month > 12))
    {
        throw new ArgumentOutOfRangeException("month", month, "month must be between 1 and 12, inclusive.");
    }

    if (/* leaving the days based on month (and perhaps leap year) calculation up to YOU! */)
    {
        throw new ArgumentOutOfRangeException("month", month, "month must be between 1 and 12, inclusive.");
    }

    this.year = year;
    this.month = month;
    this.day = day;
}

Your calling code can catch the ArgumentOutOfRangeException and notify the user of the invalid input.

Further enhancements might include using an enum to represent the valid month values rather than a numeric. Depends on your needs.

于 2013-06-14T20:53:43.413 回答