用 nextLine() 统一读取输入并手动转换类型,避免 nextInt() 留下换行符;菜单用 while (true) + break 控制流程,switch 中用 equals() 比较选项字符串,清屏优先用空行替代 ANSI 序列。
Scanner 读取用户输入时别漏掉换行符残留Java 控制台菜单最常卡在「输入完选项按回车,下一行直接跳过」——本质是 nextLine() 和 nextInt() 混用导致的缓冲区残留。比如用户输入 2 后按回车,nextInt() 只读 2,回车符留在缓冲区,紧接着的 nextLine() 立刻返回空字符串。
nextLine() 读所有输入,再手动转类型:String input = scanner.nextLine().trim();
if (input.equals("1")) { ... }
else if (input.matches("\\d+")) { int choice = Integer.parseInt(input); ... }nextInt(),之后必须加一句 scanner.nextLine(); 清掉换行符Scanner 实例,容易引发资源泄漏或输入流错乱while (true) + break 最直观不用强行套用 do-while 或状态标志变量。真实开发中,菜单退出逻辑往往不只靠一个 “0” 或 “quit”,可能涉及子菜单返回、异常中断、超时退出等。用无限循环配合明确的 break 更易读、易扩展。
while (true) {
showMainMenu();
String choice = scanner.nextLine().trim();
switch (choice) {
case "1": handleAddItem(); break;
case "2": handleViewList(); break;
case "0": System.out.println("再见"); break;
default: System.out.println("无效选项"); continue;
}
if (choice.equals("0")) break; // 显式退出条件
}break 返回上层循环,而不是靠 return 或抛异常控制流程switch 外围包 try-catch 捕获所有输入异常——它掩盖了真正该处理的业务错误(比如数字解析失败),应只捕 NumberFormatException 等具体异常equals(),不是 ==
新手常写 if (choice == "1"),结果永远进不去分支。因为 "1" 是字符串字面量,而 scanner.nextLine() 返回的是新创建的 String 对象,== 比的是引用地址,不是内容。
choice.equals("1") 或更安全的 "1".equals(choice)(后者可防 null)choice.equalsIgnoreCase("q")
choice.contains("1") 判断——用户输 "10" 也会被误判为选了选项 1System.out.print("\033[H\033[2J") 在部分终端会失效想实现“每次刷新菜单清空旧内容”,很多人搜到 ANSI 转义序列 \033[H\033[2J,但它依赖终端支持。Windows CMD 默认不识别,IDE 内置终端(如 IntelliJ 的 Terminal)也可能禁用 ANSI。
for (int i = 0; i < 20; i++) System.out.println();
if (System.getProperty("os.name").toLowerCase().contains("win")) {
// Windows 下改用 cls 命令(需 Runtime.exec,有兼容风险)
} else {
System.out.print("\033[H\033[2J");
}
含控制字符的输入、重定向文件输入时的 EOF 行为。这些不提前想清楚,上线后第一个用户就可能让程序卡死或崩溃。