【问题标题】:C# Converting values passed as arguments to variablesC# 将作为参数传递的值转换为变量
【发布时间】:2019-03-11 11:41:20
【问题描述】:

我无法理解传递给方法的变量和参数之间的关系。下面的程序应该从 main 方法中获取三个整数(MDY)并使用各种方法来验证它是否是有效日期。这包括确保年份介于 1900 和 2100 之间,以及确保月份是 1-12,并且日期在该月的天数范围内(包括闰年的 2 月 29 日)。如果 main 方法中的日期无效,程序应该这样说并打印默认日期 1/1/1900。无论提供什么参数,下面的代码总是打印默认值。我相信这是因为我使用变量MDY 或变量MonthDayYear 的方式存在问题。该程序用于分配,其中我必须使用下面代码中的所有方法和构造函数。我不确定如何将参数MDY 转换为变量MonthDayYear,因此它们可以通过ShowDate 方法打印,该方法是为我提供。

class Date
{
    private int Month;
    private int Day;
    private int Year;

    // Sets date to 1/1/1900
    public Date()
    {
        Month = 1;
        Day = 1;
        Year = 1900;
    }

    public Date(int M, int D, int Y)
    {
        SetDate(M, D, Y);
    }

    public Boolean SetDate(int M, int D, int Y)
    {

        if (ValidateDate(M, D, Y))
        {
            Month = M;
            Day = D;
            Year = Y;
            return true;

        }
        else
        {
            Console.WriteLine("Invalide date");
            SetDefaultDate();
            return false;
        }

    }

    private void SetDefaultDate()
    {
        Month = 1;
        Day = 1;
        Year = 1900;
    }
    // Determines if date is valid.
    public Boolean ValidateDate(int M, int D, int Y)
    {
        ValidateMonth();
        ValidateDay();
        ValidateYear();

        if (ValidateMonth() && ValidateDay() && ValidateYear())
        {
            ShowDate();
            return true;
        }
        else
        {
            return false;
        }


    }
        // Determines if month is valid.
    public Boolean ValidateMonth()
    {
        if (Month >= 1 && Month <= 12)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
        // Determines if year is valid.
    public Boolean ValidateYear()
    {
        if(Year >= 1900 && Year <= 2100)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
        // Determines if day is valid
    public Boolean ValidateDay()
    {
        IsLeapYear();

        if(Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12)
        {
            if (Day >= 1 && Day <= 31)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else if (Month == 4 || Month == 6 || Month == 9 || Month == 11)
        {
            if (Day >= 1 && Day <= 30)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else if (Month == 2 && IsLeapYear())
        {
            if (Day >= 1 && Day <= 29)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else if (Month == 2 && !IsLeapYear())
        {
            if (Day >= 1 && Day <= 28)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    // Determine if year is a leap year
    public Boolean IsLeapYear()
    {
        if ((Year % 4 == 0 && Year % 100 != 0) || (Year % 400 == 0))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

        // Print date to screen in format M/D/Y
    public void DisplayDate()
    {
        Console.WriteLine(ShowDate());
    }

    public String ShowDate()
    {
        StringBuilder myStringBuilder = new StringBuilder();
        myStringBuilder.AppendFormat("{0} / {1} / {2}", Month, Day, Year);
        Console.WriteLine("{0}", myStringBuilder);
        return (myStringBuilder.ToString());

    }

    static void Main(string[] args)
    {

        Date NewDate = new Date();
        NewDate.SetDate(11,11,2011);

        Console.ReadLine();
    }

}

【问题讨论】:

  • 嗨,您是否使用调试器单步执行您的代码。老实说,您的程序所采用的路径不是很“好”。对于应该是一条简单的路径,它非常复杂且不容易遵循。您在某些地方已经有了这个想法,但我认为您需要对基本的面向对象原则进行一些研究
  • 您需要根据传入的值设置字段,因此Month = M; 而不是M = Month;
  • 我不想只发布编辑后的代码,因为那不会帮助您学习。简而言之,您在 SetDate 之后使用的几乎所有代码都不接受任何参数,您所做的只是验证您在类级别字段上设置的默认值。所有验证方法都需要接受参数(M、D 和 Y),并且您需要验证您尝试设置到 Date 对象的方法,而不是您已经设置为默认值的方法
  • 你也有很多不必要的方法调用无处不在
  • 另外,ValidateDate 实际上并不会验证您传入的值。它会验证您的字段。因此,要么更改它以验证您传入的参数(如 make ValidateYear 实际上需要您传入的年份),要么在调用验证方法之前将初始值设置为您的字段。

标签: c# if-statement methods constructor arguments


【解决方案1】:

您永远不会将MDY 分配给您的MonthDayYear 字段,因此您正在检查您的默认值,默认情况下这些值都为零。您可以MDY 分配给它们相应的预期变量,但是您不会验证输入,而只是验证您的字段。相反,您可以让您的方法接受参数并检查您传递给它的内容:

public Boolean ValidateMonth(int month)
{
    if (month >= 1 && month <= 12)
    {
        return true;
    }
    else
    {
        return false;
    }
}

然后当你调用它时

 ValidateMonth(M);

然后你可以对其他两种方法做同样的事情。

此外,在您的 ValidateDate() 方法中,您还有对 ValidateMonth()ValidateDay()ValidateYear() 的三个无用调用。您将这些方法中的每一个调用两次。 (一次在开头,然后在 if 语句中再次出现。)您可以删除这些:

 public Boolean ValidateDate(int M, int D, int Y)
{
    //Remove these:
    //ValidateMonth();
    //ValidateDay();
    //ValidateYear();

    if (ValidateMonth() && ValidateDay() && ValidateYear())
    {
        ShowDate();
        return true;
    }
    else
    {
        return false;
    }


}

【讨论】:

    【解决方案2】:

    您的构造函数应该使用给定的值初始化类。目前,您的默认构造函数会初始化 MDY,但带参数的构造函数不会。

    您可以通过将构造函数更改为更像这样来解决此问题:

    public Date() : this(1,1,1900)
    {
    }
    
    public Date(int M, int D, int Y)
    {
        Month = M;
        Day = D;
        Year = Y;
    }
    

    一旦类被初始化,只需公开一个属性或方法来验证类中已经存在的值。您不需要再次将月、日和年传递给方法,因为它们已经设置好了。所以 validate 方法可能如下所示:

    public bool IsValid
    {
        get
        {
            return ValidateDay() && ValidateMonth() && ValidateYear();
        }
    }
    

    在你的主程序中:

    Date newDate = new Date(11,11,2011);
    if (newDate.IsValid)
    {
        Console.WriteLine("Date is valid.");
    }
    else
    {
        Console.WriteLine("Date is not valid.");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      • 2018-05-23
      • 2018-08-30
      相关资源
      最近更新 更多