java SE 学生管理系统总结
1.创建三个包 分别是 包1bean()
1.有个项目创建三个包 分别是包1 bean(用来存放 一些子类 父类) 包2 Main 用来存放程序的入口main方法,包3 Utlis(用来存放一些 工具类和封装起来的方法)
注意代码
一、计算年龄
用的是日历类 ,先后分别创建通过 calendar.getinstance()获取当前日历 你输入的字符串出生日期解析成 date类 再将当前日历设置成date,获取年月日,在重新获取一个日历类 获取年月日
public static int computeAge(String birthday) {
Date sBirthDay = null;
/** 将字符串解析成 date*/
try {
sBirthDay = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
} catch (ParseException e) {
e.printStackTrace();
}
/** 获取当前日历信息 并把当前解析后的日期赋值给日历对象*/
Calendar cal1 = Calendar.getInstance();
cal1.setTime(sBirthDay);
/** 抽取除当前日历的年月日*/
int birthYear = cal1.get(Calendar.YEAR);
int birthMonth = cal1.get(Calendar.MONTH);
int birthDay = cal1.get(Calendar.DAY_OF_MONTH);
/** 在重新获取当前的日历信息 并抽取出当前的年月日*/
Calendar cal2 = Calendar.getInstance();
int nowYear = cal2.get(Calendar.YEAR);
int nowMonth = cal2.get(Calendar.MONTH);
int nowDay = cal2.get(Calendar.DAY_OF_MONTH);
/**计算初步年龄*/
int age = nowYear - birthYear;
/** 在根据是否过生计算年龄*/
/** 合法性判断*/
if (birthYear > nowYear) {
System.out.println("你输入的出生日期不合法,请重新进入系统");
/*System.exit(0);*/
System.exit(0);
}
if (birthMonth > nowMonth) {
/** 说明没有过生*/
age--;
}
if ((birthDay > nowDay || birthDay == nowDay)){
/** 说明没有过生*/
age--;
}
return age;
} 二、打印出集合中学生的信息
集合中存放的是明确的类,有着明确的类名 1先判空,在遍历集合中的元素
由于集合中的是以类为基本单位 (如 添加一个学生类后 此时集合中的元素就是一个(这个集合有着若干个属性),在添加一个学生对象,集合的长度就会在增加1,对集合进行方法使用 .var的结果的类似就是 这个集合的元素所在类的类型)
public static void printList(ArrayList<Student> sList) {
/** 集合判空处理*/
if (sList.size() == 0) {
System.out.println("【查询结果】:失败,当前系统中没有任何学生信息,请添加后再查询");
}
/** 遍历集合中 对象的元素*/
for (int i = 0; i < sList.size(); i++) {
Student stu1 = sList.get(i);
System.out.println(" " + stu1.getId() + "\t\t\t\t" + stu1.getName() + "\t\t\t" +
stu1.getSex() + "\t\t\t\t" +stu1.getBirthday()+"\t\t"+stu1.getAge()
+ "\t\t\t" + stu1.show());
}
} 三、id自增
在Utils类创建静态遍历 sid = 0;在学生类的中 提供构造代码块 sid++,学生类的成员依旧是id,
==在通过set方法将键盘录入的结果处理后赋值给对象的属性是 id实现自增 stu.set(utils.sid)
public static int sid = 0;
{
Utils.sid++;
}
/** 设置id 用于自动生成*/
stu.setId(Utils.sid); 四,一些逻辑上的问题
在提示 删除/修改成功的之后 要结束方法 不然程序会在输出提示后 继续向下执行
慎用
system.exit(0); 这代码意思是直接退出 JVM,他别return要很 return 是退出当前方法
传音控股公司福利 330人发布