【发布时间】:2019-03-11 11:41:20
【问题描述】:
我无法理解传递给方法的变量和参数之间的关系。下面的程序应该从 main 方法中获取三个整数(M、D、Y)并使用各种方法来验证它是否是有效日期。这包括确保年份介于 1900 和 2100 之间,以及确保月份是 1-12,并且日期在该月的天数范围内(包括闰年的 2 月 29 日)。如果 main 方法中的日期无效,程序应该这样说并打印默认日期 1/1/1900。无论提供什么参数,下面的代码总是打印默认值。我相信这是因为我使用变量M、D、Y 或变量Month、Day、Year 的方式存在问题。该程序用于分配,其中我必须使用下面代码中的所有方法和构造函数。我不确定如何将参数M、D、Y 转换为变量Month、Day 和Year,因此它们可以通过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实际上并不会验证您传入的值。它会验证您的字段。因此,要么更改它以验证您传入的参数(如 makeValidateYear实际上需要您传入的年份),要么在调用验证方法之前将初始值设置为您的字段。
标签: c# if-statement methods constructor arguments