Java:switch内变量递增失效及重复ID检测后无法跳出case问题求助
问题解决:Java员工管理程序的两个Bug修复
问题分析与修复方案
问题1:重复ID检测后仍执行添加操作,导致对象被覆盖
原代码中检测到重复员工ID时,仅打印错误提示,但后续的薪资、姓名输入以及员工对象创建、size递增的代码依然会执行,最终新输入的员工会覆盖数组中size位置的原有对象。
修复方法:
- 新增标记位
isDuplicate,检测到重复ID时将标记设为true并跳出循环。 - 在后续添加逻辑前判断标记位,若为重复ID则直接
break跳出当前case,终止添加流程。
问题2:size变量看似未递增的假象(实际为输入逻辑混乱导致)
原代码在case 1中创建多个Scanner实例,会导致输入流读取混乱(比如nextInt()后遗留的换行符被nextLine()读取,导致姓名为空);同时因为重复ID时未终止流程,每次都执行employees[size] = ...和size++,错误的覆盖操作让你误以为size未生效。
修复方法:
- 全局仅使用一个
Scanner实例,避免多个实例争抢输入流。 - 每次调用
nextInt()/nextDouble()后,用input.nextLine()清空缓冲区,解决换行符残留问题。
修改后的完整代码
import java.util.*; public class HR { public static void main(String arg[]) throws Exception { Employee[] employees = new Employee[10]; int size = 0; int index = 0; int option = 1; int enumb = 0; int delEnumb = 0; double salary; double moreThanSalary; String name; // 全局统一使用一个Scanner实例 Scanner input = new Scanner(System.in); while (option != 0) { try { System.out.println("\n**********MENU**********"); System.out.println("1: 添加员工"); System.out.println("2: 删除员工"); System.out.println("3: 查询薪资高于指定值的员工"); System.out.println("4: 打印所有员工信息"); System.out.println("0: 退出程序"); System.out.print("\n请输入选择: "); option = input.nextInt(); // 清空nextInt后遗留的换行符 input.nextLine(); switch (option) { case 1: System.out.println("\n你选择了添加新员工"); System.out.println("\n输入员工ID: "); enumb = input.nextInt(); input.nextLine(); // 清空换行符 boolean isDuplicate = false; // 仅遍历已添加的员工,无需遍历整个数组 for (index = 0; index < size; index++) { if (employees[index].getEnumb() == enumb) { System.out.println("\n员工ID已存在"); isDuplicate = true; break; } } if (isDuplicate) { break; // 跳出当前case,终止添加流程 } System.out.println("输入薪资: "); salary = input.nextDouble(); input.nextLine(); // 清空换行符 System.out.println("输入姓名: "); name = input.nextLine(); employees[size] = new Employee(enumb, salary, name); size++; System.out.println("员工添加成功"); break; case 2: System.out.println("\n你选择了删除员工"); System.out.println("输入员工编号: "); delEnumb = input.nextInt(); input.nextLine(); boolean isDeleted = false; if (delEnumb >= 10001 && delEnumb <= 99999) { for (index = 0; index < size; index++) { if (employees[index] != null && employees[index].getEnumb() == delEnumb) { // 删除后将后续元素前移,避免数组空位置 for (int i = index; i < size - 1; i++) { employees[i] = employees[i + 1]; } employees[size - 1] = null; size--; System.out.println("员工记录已删除"); isDeleted = true; break; } } if (!isDeleted) { System.out.println("未找到该ID的员工"); } } else { System.out.println("员工ID格式错误,需在10001-99999之间"); } break; case 3: System.out.println("\n你选择了查询薪资高于指定值的员工"); System.out.println("输入薪资阈值: "); moreThanSalary = input.nextDouble(); input.nextLine(); boolean found = false; for (index = 0; index < size; index++) { if (employees[index] != null && employees[index].getSalary() > moreThanSalary) { System.out.println(employees[index]); found = true; } } if (!found) { System.out.println("没有薪资高于该值的员工"); } break; case 4: System.out.println("============================"); if (size == 0) { System.out.println("暂无员工记录"); } else { for (index = 0; index < size; index++) { if (employees[index] != null) { System.out.println(employees[index]); System.out.println("============================"); } } } break; case 0: System.out.print("\n你选择了退出程序,感谢使用!"); break; default: System.out.println("\n输入无效,请输入0-4之间的数字,重新尝试。\n"); break; } } catch (InputMismatchException ime) { System.out.print("\n\t\t\t\t ***错误*** \n输入不是有效整数,请重新尝试 :)\n"); input.nextLine(); // 清空错误输入,避免死循环 continue; } catch (EmployeeException ee) { System.out.print(ee.getMessage()); } } input.close(); // 关闭Scanner释放资源 } }
额外优化说明
- 遍历范围优化:检查员工ID、遍历员工时仅遍历已添加的
size个元素,提升执行效率。 - 删除逻辑优化:删除员工后将后续元素前移,同时
size递减,避免数组空位置影响后续操作。 - 状态提示补充:增加添加成功、未找到员工、无记录等提示,提升交互体验。
内容的提问来源于stack exchange,提问作者Lindsay Hawaii Trenton
相关产品推荐
相关产品推荐

