我正在尝试确保我的 nextday 方法可以在用户输入的日期上添加 1 天,并确保它正确添加 40 天,同时考虑到每月正确的天数和闰年
public nextday() {
for (int count = 1; count < 40; count++)
;
}
public String toDayDateString() // toDayDateString method {
int countDays = 0;
for (int i = 1; i < getMonth(); i++) {
if (i == 2 && checkLeapYr(getYear()))
countDays += 29;
else
countDays += daysPerMonth[i];
}
countDays += date;
String message = String.format("\n", countDays, getYear());
return message;
}
private Boolean checkLeapYr(int inYear) { // check leap year method.
if (getYear() % 400 == 0 || (getYear() % 4 == 0 && getYear() % 100 != 0))
return true;
else
return false;
}
下面是一个菜单,应该允许用户选择输入日期或退出,但日期未被正确接受。
{
public static void main ( String [] args)
// User selects how they will enter the dates
{
+"(1) Enter the date as MM/DD/YYYY\n"
+"(Any Key) Quit\n";
int input=0;
Date newDate;
do{
String userInput = JOptionPane.showInputDialog(null, menu);
input = Integer.parseInt( userInput);
String outputMessage="";
if ( input = 1)
{
userInput =JOptionPane.showInputDialog(null, "Please enter a date as 02/28/2011");
switch ( input ) // here is where the menu choice will be evaluated
{
case 1 :
token = userInput.split("/");
if (token.length == 3 )
{
newDate = new Date( Integer.parseInt( token[0]),
Integer.parseInt( token[1]), Integer.parseInt( token[2]) );
outputMessage = newDate.toString();
JOptionPane.showMessageDialog(null, outputMessage);
}
break;
case 2:
} while ( input <>1); // this will quit the program when user selects any key other than 1 at the menu.