我想知道如何在另一个方法中使用布尔方法的结果。以下代码包含两种方法,一种名为ValidateDay
,另一种称为IsLeapYear
。IsLeapYear
确定用户输入的整数是否为a年。ValidateDay
根据用户输入的月份数检查用户输入的日期是否为有效日期。为了检查2月29日是否是有效的一天,我需要该ValidateDay
方法来知道结果IsLeapYear
是true还是false。但是,我不确定如何IsLeapYear
在该ValidateDay
方法中引用返回值。任何建议将不胜感激。
// 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(true))
{
if (Day >= 1 && Day <= 29)
{
return true;
}
else
{
return false;
}
}
else if (Month == 2 && IsLeapYear(false))
{
if (Day >= 1 && Day <= 28)
{
return true;
}
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;
}
}
在下面的行中,将值传递true
给IsLeapYear()方法:
else if (Month == 2 && IsLeapYear(true))
但是您的IsLeapYear()方法没有任何参数,我猜测您打算在此处执行的操作是评估IsLeapYear()的结果是否为真。只需将其更改为以下内容:
else if (Month == 2 && IsLeapYear() == true)
或更简而言之:
else if (Month == 2 && IsLeapYear())
要检查该值是否为假,只需使用!要计算的表达式前的字符:
else if (Month == 2 && !IsLeapYear())
或者,如果您喜欢:
else if (Month == 2 && IsLeapYear() == false)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句