在这个程序中加入month在0-12个月之间,day在0-31天之间这样的条件,应该怎么修改?public class mydate {
private int day,month,year; //声明私有变量day,month,year
public mydate(int day,int month,int year){ //带有参数的构造函数
this.day=day;
this.month=month;
this.year=year;
}
public String tommorrow(){
day=day+1;
return day+"/"+month+"/"+year;//设置返回值
}
public static void main(String[] args) {
mydate d=new mydate(44,55,2008);//创建mydate对象
System.out.println(d.tommorrow()); //调用tommorrow()方法
}}
private int day,month,year; //声明私有变量day,month,year
public mydate(int day,int month,int year){ //带有参数的构造函数
this.day=day;
this.month=month;
this.year=year;
}
public String tommorrow(){
day=day+1;
return day+"/"+month+"/"+year;//设置返回值
}
public static void main(String[] args) {
mydate d=new mydate(44,55,2008);//创建mydate对象
System.out.println(d.tommorrow()); //调用tommorrow()方法
}}
public mydate(int day,int month,int year){ //带有参数的构造函数
if(day<0||day>31)throw NewException
else
this.day=day;
if(month<0||month>12)throw NewException
else
this.month=month;
this.year=year;
}
class NewException extends Exception
{
public NewException()
{
super();
}
}
throw new RuntimeException("不合法");
this.day=day;
this.month=month;
this.year=year;
}
你应该去看看Date,和Calendar类
System.out.println("不合法");
this.day=day;
this.month=month;
this.year=year;
} 抛出异常是为了告诉你输入不合法啊,要不你怎么知道不合法呢?打印一句话?
mydate d=new mydate(44,55,2008);直到输入的数 合法为止
throw new RuntimeException("不合法");
this.day=day;
this.month=month;
this.year=year;
}
你应该去看看Date,和Calendar类