You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Eclipse控制台无法执行Scanner用户输入操作问题求助

解决Eclipse中Scanner导致控制台无响应的问题

我来帮你排查下这个问题~你说点击运行按钮后控制台没正常运行,确实和你的Scanner使用方式以及代码里的几个小问题有关,具体分析和解决办法如下:

问题根源

  • 缺少输入提示+程序等待输入:你把提示输入的System.out.println("enter string : ");注释掉了,导致运行程序后,控制台看起来没反应,但实际上程序正卡在scanner.nextLine()这里,等待你输入字符串并回车。
  • 错误关闭标准输入流:代码里的System.in.close();完全没必要,而且非常危险——System.in是JVM全局的标准输入流,手动关闭后,整个程序甚至后续其他需要读取输入的操作都会彻底失效。

修复方案

  • 恢复输入提示语句,让你明确知道程序在等待输入;
  • 删掉System.in.close();这行代码,交给JVM自动管理标准输入流;
  • 运行程序后,在Eclipse控制台输入内容并回车,程序就会继续执行并输出结果。

修改后的完整代码

import java.util.*;
public class CandidateCode {
    public static void main(String args[]) throws Exception {
        //System.out.println("inside main");
        Scanner scanner = new Scanner(System.in);
        System.out.println("enter string : "); // 恢复输入提示
        String input = scanner.nextLine();
        //System.out.println(input);
        // 移除不必要的System.in.close()
        CandidateCode cc = new CandidateCode();
        System.out.println(cc.logic(input));
    }
    public String logic(String input) {
        String newLine= System.getProperty("line.separator");
        String output = "Hello Techgig"+newLine+ input;
        return output;
    }
}

额外小提示

如果你运行程序后找不到控制台面板,可以在Eclipse顶部菜单栏点击Window > Show View > Console调出它,确保能看到输入操作区域。

内容的提问来源于stack exchange,提问作者Vipassana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 15:07:43