该程序应该提示用户输入日期,并且它会自行增加。这是我遇到问题的 DateTest 类。错误消息显示“无法在数组类型 Date[] 上调用 nextDay()”
更新
我摆脱了不必要的陈述。但现在我收到了这个错误信息,
“错误:在类 Date 中找不到主方法,请将主方法定义为:public static void main(String[] args)”
我必须将主要方法移动到任何地方吗?
import javax.swing.JOptionPane;
public class Date {
private int month; // 1-12
private int day; // 1-31 based on month
private int year; // any year
int value;
public Date() {
month = 1;
day = 1;
year = 1900;
}
public Date(int m, int d, int y) {
month = m;
year = y;
day = d;
}
public String GetDate() {
String Msg1 = month + "/" + day + "/" + year;
JOptionPane.showMessageDialog(null, Msg1);
return Msg1;
}
public void setMonth() {
int value = Integer.parseInt(JOptionPane.showInputDialog("Enter Month:"));
if (value > 1 && value < 13) // validate month
{
month = value;
} // check for leap year
else if (month == 2) {
boolean isleap = true;
if (year % 4 != 0) {
isleap = false;
} else {
isleap = true;
}
} else // month is invalid
{
String Message = "Month" + month + "Month must be 1-12";
JOptionPane.showMessageDialog(null, Message);
}
}
public void setDay() {
int value2 = Integer.parseInt(JOptionPane.showInputDialog("Enter Day:"));
int[] daysPerMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// check if day in range for month
if (value2 > 0 && value2 <= daysPerMonth[month]) {
day = value2;
}
value = value2;
}
public void setYear() {
int value3 = Integer.parseInt(JOptionPane.showInputDialog("Enter Year:"));
year = value3;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public int getYear() {
return year;
}
public int nextDay() {
int cDay = day + 1;
if (value == cDay) {
day = cDay;
} else {
day = 1;
}
{
NextMonth();
}
return day;
}
public int NextMonth() {
if (12 == month) {
year++;
}
return month = 1;
}
public String toString() {
return month + "/" + day + "/" + year;
}
}
class dateTest {
public static void main(String args[]) {
Date newDate = new Date(11, 27, 2011);
final int arraySize = 1;
{
for (int i = 0; i < arraySize; i++) {
newDate.setDay();
newDate.setMonth();
newDate.setYear();
}
for (int counter = 0; counter < 4; counter++) {
newDate.nextDay();
{
String Message = "Incremented Date:" + newDate.toString();
{
JOptionPane.showMessageDialog(null, Message);
System.exit(0);
}
}
}
}
}
}