3-8作业问题
package com.imcoo.demo; /*** * 创建 部门类 * @author Administrator * 描述部门编号、部门名称、获取所有该部门员工信息 * 统计该部门员工个数 */ public class Department { private String departmentnumb; private String departmentname; private staff[] mystaff; private int staffNumber; public Department() { } public Department(String departmentname) { this.setDepartmentname(departmentname); } public String getDepartmentnumb() { return departmentnumb; } public void setDepartmentnumb(String departmentnumb) { this.departmentnumb = departmentnumb; } public String getDepartmentname() { return departmentname; } public void setDepartmentname(String departmentname) { this.departmentname = departmentname; } public staff[] getMystaff() { if(this.mystaff==null) this.mystaff=new staff[100]; return mystaff; } public void setMystaff(staff[] mystaff) { this.mystaff = mystaff; } public int getStaffNumber() { return staffNumber; } public void setStaffNumber(int staffNumber) { this.staffNumber = staffNumber; } public void addstaff(staff sta) { for(int i=0,m=0,n=0;i<this.getMystaff().length;i++) { if(this.getMystaff()[i]==null) { this.getMystaff()[i]=sta; if(this.getMystaff()[i].getStaffname()=="人事部") { this.staffNumber=m+1; m++; } else {this.staffNumber=n+1; n++;} break; } } } } package com.imcoo.test; import com.imcoo.demo.Department; import com.imcoo.demo.Job; import com.imcoo.demo.staff; public class TestDemo { public static void main(String[] args) { // TODO Auto-generated method stub Department dep1=new Department("人事部"); Job job1=new Job("经理"); staff sta1= new staff("张铭","S001","男",29,dep1,job1); System.out.println(sta1.Play()); System.out.println("===================================="); Job job2=new Job("助理"); staff sta2= new staff("李艾爱","S002","女",21,dep1,job1); System.out.println(sta2.Play()); System.out.println("===================================="); Job job3=new Job("职员"); staff sta3= new staff("孙超","S004","男",29,dep1,job3); System.out.println(sta3.Play()); System.out.println("===================================="); Department dep2=new Department("市场部"); staff sta4= new staff("张美美","S005","女",26,dep2,job3); System.out.println(sta4.Play()); System.out.println("===================================="); staff sta5= new staff("张蓝迪","S006","男",37,dep2,job1); System.out.println(sta5.Play()); staff sta6= new staff("米莉","S007","女",24,dep2,job3); System.out.println(sta6.Play()); System.out.println("===================================="); dep1.addstaff(sta1); dep1.addstaff(sta2); dep1.addstaff(sta3); dep2.addstaff(sta4); dep2.addstaff(sta5); dep2.addstaff(sta6); System.out.println(dep1.getDepartmentname()+"总共有"+dep1.getStaffNumber()+"名员工"); System.out.println(dep2.getDepartmentname()+"总共有"+dep2.getStaffNumber()+"名员工"); } }
老师帮忙看下 我这个输出为什么一直是1 到底是我的加法语句有问题 还是其他地方的问题
12
收起
正在回答 回答被采纳积分+1
1回答
好帮手慕雪
2017-09-27 09:48:56
1)因为每次 addstaff(staff sta) 时都会走 for(int i=0,m=0,n=0;i<this.getMystaff().length;i++) {所以m和n每次都是从0开始计数的,你可以把m,n删除,直接写成this.staffNumber=this.staffNumber+1; 2)另外你没必要 if(this.getMystaff()[i].getStaffname()=="人事部"){}else{}不管是什么部门,this.staffNumber都是在统计本部门的员工数。直接this.staffNumber=this.staffNumber+1; 就可以了。3)字符串判断内容是否相等不是用==判断,而应该用"".equals("")这种形式。祝:学习愉快
Android零基础入门2018版
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7238 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星