java怎么通过出生年月获得五个学生的年龄并计算他们的平均年龄?
public class LX_1 {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
double average,sum=0;
int x,y,z,a,b,c;
double[] ages = new double[5];
for(int i=0;i<5;i++) {
System.out.print("依次输入第"+(i+1)+"个同学的出生年月日:");
x=input.nextInt();
y=input.nextInt();
z=input.nextInt();
System.out.print("依次输入第"+(i+1)+"个同学现在的年月日:");
a=input.nextInt();
b=input.nextInt();
c=input.nextInt();
ages[i] = age(x,y,z,a,b,c);
}
for(int i=0;i<5;i++) {
System.out.println("第"+i+"位同学的年龄为:"+ages[i]);
sum += ages[i];
}
System.out.println("这五位同学的平均年龄为:" + (sum/5.0));
}
public static double age(int x,int y,int z,int a,int b,int c) {
int year=0,month=0,day = 0;
double age;
if(a>=x){
if(b>=y){
if(c>=z){
year=a-x;month=b-y;day=c-z;
}
else{
year=a-x;month=b-y-1;
if(b==4 || b==6 || b==9 || b==10){
day=30-z+c;
}
else if(b==2){
day=28-z+c;
}
else
day=31-z+c;
}
}
else{
if(c>=z){
year=a-x;month=12-y+b;day=c-z;
}
else{
year=a-x;month=12-y+b-1;
if(b==4 || b==6 || b==9 || b==10){
day=30-z+c;
}
else if(b==2){
day=28-z+c;
}
else
day=31-z+c;
}
}
}
age = year + month/12.0 + day/30.0;
return age;
}
}