我在这里寻找类似的东西,但似乎没有任何帮助。我正在尝试将输入到我的布尔方法中的值返回给我的测试程序。任何建议都会被排除在外。由于我的代码很长,我只会包含我认为相关的内容。
public class SeasonCalculatorTester
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the month: ");
int month = in.nextInt();
System.out.print("Enter the day: ");
int day = in.nextInt();
SeasonCalculator calculator = new SeasonCalculator(month, day);
//TODO USE THE calculator (SeasonCalculator) TO CHECK FOR VALID MONTH AND VALID DAY.
// IF MONTH OR DAY IS NOT VALID PRINT ERROR MESSAGE.
// IF VALID PRINT OUT THE MONTH (IN STRING FORM, NOT INT FORM), THE DAY,
// AND THE SEASON THAT THE DAY IS IN
if(calculator.isValidMonth())
{
System.out.print(calculator.getMonth());
}
if(calculator.isValidDay())
{
System.out.print(" "+calculator.isValidDay()+" is in the "+ calculator.getSeason());
}
else
{
System.out.println("False");
}
}
}
我需要返回 int 值的方法是“isValidDay()”,代码如下:
public boolean isValidDay()
{
if ((month == 1 && day >= 1) && (month == 1 && day <= 31))
{
return true;
}
if ((month == 2 && day >= 1) && (month == 2 && day <= 29))
{
return true;
}
if ((month == 3 && day >= 1) && (month == 3 && day <= 31))
{
return true;
}
if ((month == 4 && day >= 1) && (month == 4 && day <= 30))
{
return true;
}
if ((month == 5 && day >= 1) && (month == 5 && day <= 31))
{
return true;
}
if ((month == 6 && day >= 1) && (month == 6 && day <= 30))
{
return true;
}
if ((month == 7 && day >= 1) && (month == 7 && day <= 31))
{
return true;
}
if ((month == 8 && day >= 1) && (month == 8 && day <= 31))
{
return true;
}
if ((month == 9 && day >= 1) && (month == 9 && day <= 30))
{
return true;
}
if ((month == 10 && day >= 1) && (month == 10 && day <= 31))
{
return true;
}
if ((month == 11 && day >= 1) && (month == 11 && day <= 31))
{
return true;
}
if ((month == 12 && day >= 1) && (month == 12 && day <= 31))
{
return true;
}
else
{
return false;
}
}