java吧 关注:1,240,095贴子:12,711,755
  • 0回复贴,共1

求大神指教。

只看楼主收藏回复

package cn.edu.qfnu.ex2_3;
import java.util.Calendar;
/**
* 设计一个Birthday类,成员变量有year, month, day。 提供构造方法、计算下一天的方法和计算年龄的方法
*/
public class birthday {
private int year;
private int month;
private int day;
// 构造方法
public birthday(int year, int month, int day) {
super();
this.year = year;
this.month = month;
this.day = day;
}
// get set方法
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int computeAge() {
Calendar calendar = Calendar.getInstance();
int Year = calendar.get(Calendar.YEAR);
return Year - year;
}
public int nextday() {
switch (month) {
case 1:
if (day <= 30) {
day = day + 1;
} else
month = month + 1;
day = 1;
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (day <= 30) {
day = day + 1;
} else
month = 1;
day = 1;
case 4:
if(month<=29){
day=day+1;
}else
month=month+1;
day=1;
case 6:
case 9:
case 11:
case 2:
if(year%400==0||year%4==0&&year%100!=0){
if(day<=28){
day=day+1;
}else
month=month+1;
day=1;
}else if(day<=27){
day=day+1;
}else
month=month+1;
day=1;
}
return year+month+day;
}
public static void main(String[] args) {
birthday bir=new birthday(1994,8,5);
System.out.println("年龄为"+bir.computeAge()+" "+"下一天为"+bir.nextday());
}
}
本人菜鸟,求大神改错。。


IP属地:山东1楼2013-11-27 13:05回复