
本文详解如何用健壮的 while 循环替代错误嵌套 for 循环,实现用户交互式选取6个不重复神祇对象的功能,避免索引越界、重复选取和提前退出等问题。
在 Java 游戏模拟器类项目中,常需让用户从预加载的神祇(God)列表中选择固定数量(如 6 个)角色组成队伍。原始实现中使用了 for (int i = 0; i ailable) 重输逻辑——这导致严重逻辑缺陷:内层 j 循环未完成就中断,且每次检查仅对比当前索引 j 处的神祇 ID,而非全表搜索;更危险的是,在 listOfAllGods.remove(j) 后继续 j++ 会跳过下一个元素(因列表收缩),极易漏检或抛出 IndexOutOfBoundsException。
✅ 正确解法是放弃固定次数的 for 循环,改用条件驱动的 while 循环,以 selectedGods.size()
以下是重构后的 selectGodsForTeam() 核心逻辑(已修复所有关键问题):
void selectGodsForTeam() {
Scanner scanner = new Scanner(System.in);
System.out.println("Please choose the 6 id's of the gods you wish to pick:");
while (selectedGods.size() < 6) {
System.out.print("You have selected " + selectedGods.size()
+ " god(s). Enter ID of next god > ");
int chooseGodId;
try {
chooseGodId = scanner.nextInt();
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a valid number.");
scanner.nextLine(); // consume invalid token
continue;
}
// 检查是否已选过该神祇(防重复)
if (findGod(selectedGods, chooseGodId) >= 0) {
System.out.println("❌ God ID " + chooseGodId + " is already selected. Please choose another.");
continue;
}
// 在剩余神祇中查找目标 ID
int foundIndex = findGod(listOfAllGods, chooseGodId);
if (foundIndex == -1) {
System.out.println("❌ God ID " + chooseGodId + " is not available. Please choose again.");
continue;
}
// 安全添加并移除
selectedGods.add(listOfAllGods.get(foundIndex));
listOfAllGods.remove(foundIndex); // ArrayList.remove(int index) 是 O(n),但数据量小可接受
System.out.println("✅ Added: " + listOfAllGods.get(foundIndex).getName()
+ " (ID: " + chooseGodId + ")");
}
System.out.println("\n? Team selection complete! You've chosen "
+ selectedGods.size() + " gods.");
}配套的 findGod 工具方法(建议定义为私有辅助方法):
private int findGod(ListgodList, int targetId) { for (int i = 0; i < godList.size(); i++) { if (godList.get(i).getId() == targetId) { return i; } } return -1; // not found }
? 关键改进说明:
- ✅ 语义清晰:while (selectedGods.size() 编码循环次数;
- ✅ 单次输入,全局验证:每次输入后,先查是否已选(去重),再查是否存在于可用池(有效性),逻辑正交无耦合;
- ✅ 安全移除:通过 findGod 获取确切索引后再 remove(index),避免遍历时修改列表引发的并发修改异常或索引错位;
- ✅ 健壮输入处理:捕获 InputMismatchException 防止非数字输入崩溃,并清空非法输入缓存;
- ✅ 即时反馈:成功/失败均给出明确提示,提升用户体验。
⚠️ 注意事项:
- 不要在遍历 ArrayList 时直接调用 remove(Object) 或在增强 for 循环中修改集合——本方案通过先查索引、后移除,完全规避此风险;
- 若未来神祇数量极大(>10⁴),可考虑用 HashMap
替代 List 存储 listOfAllGods,将查找复杂度从 O(n) 降至 O(1); - getSelectedGods() 方法当前返回的是 listOfAllGods(明显笔误),应修正为 return new ArrayList(selectedGods); 以防止外部篡改。
遵循此模式,即可稳定、可扩展地支持任意数量的神祇选取逻辑,为后续战斗系统奠定坚实基础。










