老师好,统计部门人数和空指针的问题。
老师你好,统计部门人数和空指针,调了很久实在做不出来,请问下这两个怎么做呢?哭戚戚
另外我的代码还有什么地方需要修改呢
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | package com.fiona.javaObjectOriented.day12office; /** * 部门类 */ public class BuMen { // 成员属性:部门编号、部门名称、员工数组、统计部门中的员工个数(统计变量) private String bmId; private String bmName; private YuanGong [] myYuanGong; private int bmNum; //无参构造 public BuMen(){ } //带参构造 public BuMen(String bmId, String bmName) { this .setBmId(bmId); this .setBmName(bmName); } public String getBmId() { return bmId; } public void setBmId(String bmId) { this .bmId = bmId; } public String getBmName() { return bmName; } public void setBmName(String bmName) { this .bmName = bmName; } public YuanGong[] getMyYuanGong() { return myYuanGong; } public void setMyYuanGong(YuanGong[] myYuanGong) { this .myYuanGong = myYuanGong; } public int getBmNum() { return bmNum; } public void setBmNum( int bmNum) { this .bmNum = bmNum; } //统计该部门员工个数 public void addNum(YuanGong yg){ for ( int i= 0 ;i< this .getMyYuanGong().length;i++){ if ( this .getMyYuanGong()[i]== null ){ this .getMyYuanGong()[i]=yg; this .bmNum=i+ 1 ; return ; } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | package com.fiona.javaObjectOriented.day12office; /** * 职务类 */ public class ZhiWu { //职务编号、职务名称 private String zwId; private String zwName; //无参构造方法 public ZhiWu(){ } //有参构造方法:包含职务编号、职务名称 public ZhiWu(String zwId, String zwName) { this .setZwId(zwId); this .setZwName(zwName); } public String getZwId() { return zwId; } public void setZwId(String zwId) { this .zwId = zwId; } public String getZwName() { return zwName; } public void setZwName(String zwName) { this .zwName = zwName; } public String info(){ String str= "职务信息如下:\n职务编号:" + this .getZwId()+ "\n职务名称:" + this .getZwName(); return str; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | package com.fiona.javaObjectOriented.day12office; /** * 员工类: */ public class YuanGong { //员工姓名、工号、年龄、性别、所属部门、职务信息 private String ygNmae; private String ygID; private int ygAge; private String ygSex; private BuMen ygbumen; private ZhiWu ygzhiwu; //无参构造方法 public YuanGong(){} //有参构造方法:工姓名、工号、性别、年龄、部门信息、职务信息 public YuanGong(String ygNmae, String ygID, int ygAge, String ygSex,BuMen ygbumen,ZhiWu ygzhiwu) { this .setYgNmae(ygNmae); this .setYgID(ygID); this .setYgAge(ygAge); this .setYgSex(ygSex); this .setYgbumen(ygbumen); this .setYgzhiwu(ygzhiwu); } public String getYgNmae() { return ygNmae; } public void setYgNmae(String ygNmae) { this .ygNmae = ygNmae; } public String getYgID() { return ygID; } public void setYgID(String ygID) { this .ygID = ygID; } public int getYgAge( int ygAge) { return ygAge; } //设定方法限定年龄只能是18--65之间,反之则设置默认为18岁 public void setYgAge( int ygAge) { if (ygAge< 18 ||ygAge> 65 ) this .ygAge= 18 ; else this .ygAge=ygAge; } public String getYgSex() { return ygSex; } //设定方法限定性别只能是“男”或者“女”,反之则设置默认为"男" public void setYgSex(String ygSex) { if (ygSex== "男" ||ygSex== "女" ) this .ygSex = ygSex; else this .ygSex= "男" ; } public int getYgAge() { return ygAge; } public BuMen getYgbumen() { return ygbumen; } public void setYgbumen(BuMen ygbumen) { this .ygbumen = ygbumen; } public ZhiWu getYgzhiwu() { return ygzhiwu; } public void setYgzhiwu(ZhiWu ygzhiwu) { this .ygzhiwu = ygzhiwu; } //员工自我介绍的方法,将员工信息作为字符串返回 public String info( ){ String str = "姓名:" + this .getYgNmae()+ "\n工号:" + this .ygID+ "\n性别:" + this .ygSex+ "\n年龄:" + this .ygAge+ "\n职务:" + this .ygbumen.getBmName()+ this .ygzhiwu.getZwName(); return str; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | package com.fiona.javaObjectOriented.day12officeTest; import com.fiona.javaObjectOriented.day12office.BuMen; import com.fiona.javaObjectOriented.day12office.YuanGong; import com.fiona.javaObjectOriented.day12office.ZhiWu; public class Test { public static void main(String[] args) { //初始化部门 BuMen bm1= new BuMen( "D001" , "人事部" ); BuMen bm2= new BuMen( "D002" , "市场部" ); //初始化职务 ZhiWu zw1 = new ZhiWu( "P001" , "经理" ); ZhiWu zw2 = new ZhiWu( "P002" , "助理" ); ZhiWu zw3 = new ZhiWu( "P003" , "职员" ); //初始化员工 YuanGong yg1 = new YuanGong( "张铭" , "S001" , 29 , "男" ,bm1,zw1); YuanGong yg2 = new YuanGong( "李艾爱" , "S002" , 21 , "女" ,bm1,zw2); YuanGong yg3 = new YuanGong( "孙超" , "S003" , 29 , "男" ,bm1,zw3); YuanGong yg4 = new YuanGong( "张美美" , "S004" , 26 , "女" ,bm2,zw3); YuanGong yg5 = new YuanGong( "蓝迪" , "S005" , 37 , "男" ,bm2,zw1); YuanGong yg6 = new YuanGong( "米莉" , "S006" , 24 , "女" ,bm2,zw3); //输出员工信息 System.out.println(yg1.info()+ "\n==============================" ); System.out.println(yg2.info()+ "\n==============================" ); System.out.println(yg3.info()+ "\n==============================" ); System.out.println(yg4.info()+ "\n==============================" ); System.out.println(yg5.info()+ "\n==============================" ); System.out.println(yg6.info()+ "\n==============================" ); //人事部添加员工 bm1.addNum(yg1); bm1.addNum(yg2); bm1.addNum(yg3); System.out.println(bm1.getBmName()+ "总共有" +bm1.getBmNum()+ "名员工" ); //市场部添加员工 bm1.addNum(yg4); bm1.addNum(yg4); bm1.addNum(yg4); System.out.println(bm2.getBmName()+ "总共有" +bm2.getBmNum()+ "名员工" ); } } |
24
收起
正在回答 回答被采纳积分+1
1回答
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧