【发布时间】:2011-10-26 01:45:54
【问题描述】:
大家好,再次感谢您抽出宝贵时间查看我的问题。
我正在尝试创建一个程序来跟踪员工和他们所在的不同部门。
程序首先从一个文本文件中读取所有初始数据以使程序运行。然后程序在一个while循环中有一个while循环。第一个循环会读取部门详细信息,然后创建部门
然后在下一个(内部)while循环中读取与该部门相关的所有员工,然后在读取员工的详细信息后创建员工并将其添加到之前创建的部门中,说这是我工作的部门在。
在将所有员工添加到部门后,它会退出该内部循环,并将部门及其内部的员工发送到 mainDriver 进行存储。它为其余部门再次添加其关联员工等执行此操作。
问题是:似乎创建每个部门都可以,并添加到mainDriver,但所有员工都添加到第一个部门,其余部门为空。这不是应该的工作方式,因为每个部门都有几名员工。
为什么在实例化新部门时它不移动到下一个部门??
能否请我帮忙看看哪里出错了。
这是读取数据的代码。
while (index < numberOfDepartmentsToRead )
{
String depName1 = inFile.nextLine();
String location1 = inFile.nextLine();
String numberOfEmps = inFile.nextLine();
int numberOfEmps1 = Integer.parseInt(numberOfEmps);
Department newDepartment = new Department(depName1 , location1);
while (i < numberOfEmps1 )
{
String fName = inFile.nextLine();
String lName = inFile.nextLine();
String gender = inFile.nextLine();
String address = inFile.nextLine();
String payLevel = inFile.nextLine();
int dPayLevel = Integer.parseInt(payLevel);
Employee employeesFromList = new Employee(randomIDno, fName, lName, gender, dPayLevel);
newDepartment.setAddEmp(employeesFromList, randomIDno);
i++;
}
i = 0;
index++;
MainDriver.setDepartmentToSystem(newDepartment);
}
员工被传递给部门类中的这个方法
public static void setAddEmp(Employee theEmp, int idNumber)
{
employeesInThisDepartment.add(theEmp);
employeeMap.put(idNumber, theEmp);
}
部门添加到mainDriver类的存储方法就是这个
public static void setDepartmentToSystem(Department theDepartment)
{
allDepartments.add(theDepartment);
}
【问题讨论】:
-
我假设 i 在第一个循环之前被初始化为零?
-
我重复我前几天的问题——你使用什么资源来学习 Java?似乎很多问题都是基本的语言级问题,可以通过 Java 基础概述快速解决。
标签: java while-loop instantiation setter