扫描仪不会接收输入,因此无法处理

时间:2014-03-31 23:17:21

标签: java input

此类运行但是当我在扫描仪中输入内容时没有任何反应。它应该注册输入并运行另一个类,然后我得到响应并尝试打印它。

public class Eliza {
    public static void main(String[] args) {
        String line = "";
        String response = "";
        ElizaResponder responder = new ElizaResponder();

        System.out.println("Welcome to Christian Rebelo's Eliza");
        System.out.println("---------------------------");
        System.out.println("ELIZA> " + responder.getGreeting());

        Scanner s = new Scanner(System.in);

        while (responder.isNotFinished()) {
            line = s.nextLine();
        }

        response = responder.getResponse(line);
        System.out.println(response);

        System.out.println("ELIZA> " + responder.getFarewell());
    }
}

2 个答案:

答案 0 :(得分:1)

根据评论来判断:

while (responder.isNotFinished()) {
    line = s.nextLine();
}
    response = responder.getResponse(line);
    System.out.println(response);

应该是这样的:

while (responder.isNotFinished()) {
    line = s.nextLine();

    response = responder.getResponse(line);
    System.out.println(response);
}

答案 1 :(得分:0)

此:

while (responder.isNotFinished()) {
        line = s.nextLine();
}

根本不会读取任何行,或者在没有输入任何内容的情况下读取无数行。