Java学校作业员工管理菜单系统数组传参异常故障求助
问题排查与修正方案
核心错误列表
- 员工数组生命周期错误:main方法中
Employee[] employees = new Employee[100];定义在while循环内部,每次菜单循环都会创建全新的空数组,之前加载/新增的员工数据会全部丢失 - 数组下标使用错误:loadData方法中员工从下标
i+1开始存储,不仅浪费了下标0的位置,当加载100个员工时会触发数组下标越界异常;addEmployee方法固定将新员工存在下标0,会覆盖之前存储的员工 - 空指针未处理:
specificEmployee、salaryRange方法遍历数组时没有判断当前元素是否为null,直接访问属性会触发空指针异常 - 展示逻辑错误:
displayEmployees方法注释了逐行输出单个员工的逻辑,改为输出整个数组的字符串形式,不符合功能要求 - Scanner使用不规范:创建了多个Scanner实例,且nextInt读取后残留的换行符会导致后续nextLine读取姓名时拿到空字符串
- 错误提示逻辑错误:
specificEmployee和salaryRange方法每次遍历不匹配就输出错误提示,会出现大量重复的无效提示,应该遍历完所有元素都没匹配到再输出一次即可
前置依赖补充
如果尚未定义Employee类,需要先补充:
class Employee { String name; int id; int salary; public Employee(String name, int id, int salary) { this.name = name; this.id = id; this.salary = salary; } }
修正后完整代码
package practice; import java.util.Scanner; public class Project { // 加载员工数据,传入现有数组和当前员工数,返回新增后的员工总数 public static int loadData(Employee[] employees, int currentCount) { Scanner scan = new Scanner(System.in); System.out.println("请输入要加载的员工数量:"); int numberEmployees = scan.nextInt(); // 防止超出数组最大容量100 if (currentCount + numberEmployees > 100) { System.out.println("最多只能存储100名员工,当前剩余容量:" + (100 - currentCount)); return currentCount; } for (int i = 0; i < numberEmployees; i++) { scan.nextLine(); // 吃掉nextInt残留的换行符 System.out.println("输入第" + (i+1) + "位员工姓名:"); String empName = scan.nextLine(); System.out.println("输入5位数字id:"); int empID = scan.nextInt(); System.out.println("输入年薪:"); int empSalary = scan.nextInt(); employees[currentCount + i] = new Employee(empName, empID, empSalary); } return currentCount + numberEmployees; } public static Employee addEmployee() { Scanner scan = new Scanner(System.in); scan.nextLine(); // 吃掉nextInt残留的换行符 System.out.println("输入姓名:"); String empName = scan.nextLine(); System.out.println("输入5位数字id:"); int empID = scan.nextInt(); System.out.println("输入年薪:"); int empSalary = scan.nextInt(); return new Employee(empName, empID, empSalary); } public static void displayEmployees(Employee[] employees, int empCount) { System.out.println("=== 所有员工信息 ==="); for (int i = 0; i < empCount; i++) { System.out.println("员工姓名:" + employees[i].name + " ID:" + employees[i].id + " 年薪:" + employees[i].salary); } } public static void specificEmployee(Employee[] employees, int empCount, int id) { boolean found = false; for (int i = 0; i < empCount; i++) { if (employees[i].id == id) { System.out.println("姓名:" + employees[i].name + " ID:" + employees[i].id + " 年薪:" + employees[i].salary); found = true; break; } } if (!found) { System.out.println("ID不被识别"); } } public static void salaryRange(Employee[] employees, int empCount, int salaryMinimum, int salaryMaximum) { boolean found = false; System.out.println("=== 薪资区间[" + salaryMinimum + "," + salaryMaximum + "]的员工信息 ==="); for (int i = 0; i < empCount; i++) { if (employees[i].salary >= salaryMinimum && employees[i].salary <= salaryMaximum) { System.out.println("姓名:" + employees[i].name + " ID:" + employees[i].id + " 年薪:" + employees[i].salary); found = true; } } if (!found) { System.out.println("没有符合薪资区间的员工"); } } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int menuChoice = 0; // 数组和员工计数移到循环外部,保证数据不会被重置 Employee[] employees = new Employee[100]; int empCount = 0; // 记录当前已存储的员工总数 while (menuChoice != 6) { System.out.println("\t菜单:"); System.out.println("1. 加载员工数据"); System.out.println("2. 新增员工"); System.out.println("3. 展示所有员工"); System.out.println("4. 查询指定员工信息"); System.out.println("5. 查询指定薪资区间员工"); System.out.println("6. 退出"); menuChoice = scan.nextInt(); if (menuChoice == 1) { empCount = loadData(employees, empCount); } else if (menuChoice == 2) { if (empCount >= 100) { System.out.println("员工容量已达上限,无法新增"); continue; } employees[empCount] = addEmployee(); empCount++; } else if (menuChoice == 3) { if (empCount == 0) { System.out.println("暂无员工数据"); continue; } displayEmployees(employees, empCount); } else if (menuChoice == 4) { if (empCount == 0) { System.out.println("暂无员工数据"); continue; } System.out.println("输入5位员工ID:"); int id = scan.nextInt(); specificEmployee(employees, empCount, id); } else if (menuChoice == 5) { if (empCount == 0) { System.out.println("暂无员工数据"); continue; } System.out.println("输入薪资区间最小值:"); int salaryMinimum = scan.nextInt(); System.out.println("输入薪资区间最大值:"); int salaryMaximum = scan.nextInt(); salaryRange(employees, empCount, salaryMinimum, salaryMaximum); } else if (menuChoice == 6) { System.out.println("退出系统"); break; } else { System.out.println("无效选择,请重新输入"); } } scan.close(); } }
内容的提问来源于stack exchange,提问作者ahap13
相关产品推荐
相关产品推荐

