java知道学生的学号,姓名,年龄,性别出生年月去求年龄和男生女生人数,为什么结果不对啊
代码发在下面
把你代码放出来啊,不然怎么知道问题
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Student {
String stuNum;
String name;
String sex;
// Date birthDate;
String birthYearMonth;
// 2000-01
public static final String MALE="男";
public static final String FEMALE="女";
//格式:年-月
public static final String fmtYm = "yyyy-MM";
/**
* 把符合日期格式的字符串转换为日期类型
*/
public static Date stringtoDate(String dateStr, String format) {
Date d = null;
SimpleDateFormat formater = new SimpleDateFormat(format);
try {
formater.setLenient(false);
d = formater.parse(dateStr);
} catch (Exception e) {
// log.error(e);
d = null;
}
return d;
}
/**
* 返回日期的年
*/
public static int getYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.YEAR);
}
// https://blog.csdn.net/kiss_the_sun/article/details/6831051
public int getAge(){
// Date date = DateUtil.stringtoDate(birthYearMonth, DateUtil.MONTG_DATE_FORMAT);
Date date = stringtoDate(birthYearMonth,fmtYm);
Date now = new Date();
// Date.
int yearNow = getYear(now);
int yearBirth = getYear(date);
return yearNow-yearBirth;
// return DateUtil.dayDiff(now,date);
// return now-date;
// Date date=new Date(Long.parseLong(time));
// SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// time=formatter.format(date);
//————————————————
// 版权声明:本文为CSDN博主「kiss_the_sun」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
// 原文链接:https://blog.csdn.net/kiss_the_sun/article/details/6831051
}
}
import java.util.List;
public class StuClass {
String clsNum;
String name;
// Student
List<Student> students;
public double avgAge(){
double avg=0;
for (Student student : students) {
avg+=student.getAge();
}
return avg/students.size();
}
public int cntOfMale(){
int cnt=0;
for (Student student : students) {
// if(student.sex.equals("男人")){
// if("男".equals(student.sex)){
if(Student.MALE.equals(student.sex)){
cnt++;
}
}
return cnt;
}
public int cntOfFemale(){
int cnt=0;
for (Student student : students) {
// if(student.sex.equals("男人")){
// if("女".equals(student.sex)){
if(Student.FEMALE.equals(student.sex)){
cnt++;
}
}
return cnt;
}
}
import java.util.List;
public class School {
List<StuClass> stuClasses;
public double avgClassStuCnt(){
double cntAll=0;
for (StuClass stuClass : stuClasses) {
cntAll+=stuClass.students.size();
}
return cntAll/stuClasses.size();
}
}
import java.util.ArrayList;
public class Cal {
static void calSchool() {
School school = new School();
school.stuClasses = new ArrayList<>();
for (int i = 0; i < 3; i++) {
school.stuClasses.add(mockClass(i));
}
double avgClassStuCnt = school.avgClassStuCnt();
System.out.println("avgClassStuCnt");
System.out.println(avgClassStuCnt);
}
static StuClass mockClass(int classNum) {
StuClass stuClass = new StuClass();
// stuClass.clsNum="20190911";
// stuClass.name="信管191";
stuClass.clsNum = "2019091" + classNum;
stuClass.name = "信管19" + classNum;
stuClass.students = new ArrayList<>();
// for (int i = 0; i <3 ; i++) {
for (int i = 0; i < 6; i++) {
Student student = new Student();
student.stuNum = "2019090" + i;
student.name = "好人" + i;
// student.sex="男人";
student.sex = Student.MALE;
// student.birthDate=new Date();
student.birthYearMonth = "2000-0" + (i + 1);
stuClass.students.add(student);
}
return stuClass;
}
static void testOneClass() {
StuClass stuClass = new StuClass();
stuClass.clsNum = "20190911";
stuClass.name = "信管191";
stuClass.students = new ArrayList<>();
// for (int i = 0; i <3 ; i++) {
for (int i = 0; i < 6; i++) {
Student student = new Student();
student.stuNum = "2019090" + i;
student.name = "好人" + i;
// student.sex="男人";
student.sex = Student.MALE;
// student.birthDate=new Date();
student.birthYearMonth = "2000-0" + (i + 1);
stuClass.students.add(student);
}
double avgAge = stuClass.avgAge();
System.out.println("avgAge of class : " + stuClass.name);
System.out.println(avgAge);
// 平均年龄
// stuClass.
int cntOfFemale = stuClass.cntOfFemale();
int cntOfMale = stuClass.cntOfMale();
System.out.println("cntOfFemale");
System.out.println(cntOfFemale);
System.out.println("cntOfMale");
System.out.println(cntOfMale);
// 输出
// avgAge of class : 信管191
// 22.0
// cntOfFemale
// 0
// cntOfMale
// 6
}
public static void main(String[] args) {
calSchool();
// 第四题
// avgClassStuCnt
// 6.0
}
}