Java交互输入EOF

发布于 2024-11-06 19:09:41 字数 1088 浏览 3 评论 0原文

我正在尝试为棋盘游戏实现用户输入界面。我试图一次获取一个用户输入,然后将其写入文件(因为我需要保存用户所做的移动列表)。到目前为止,我所拥有的运行良好(读取输入并将其写入文件),但是,每当用户想要停止输入时,程序就会停止工作。 IE;当你按下ctrl+c时,程序就结束了。

这是我到目前为止所拥有的, fileName 变量已在主函数之外声明,

public static void main(String[] args) throws IOException {
    BufferedReader inputReader = new BufferedReader (new InputStreamReader (System.in));
    try {
        FileWriter outFile = new FileWriter (fileName);
        PrintWriter out = new PrintWriter (outFile);
        System.out.print ("Enter move: ");
        String line = inputReader.readLine();
        while (line != null) {
            System.out.print ("Enter move: ");
            out.write(inputReader.readLine());
            out.write(" ");
            }
            out.close();
    } catch (IOException e) {
        System.out.println (e.getMessage());
    }

    System.out.println ("Reached here");
}

我想做的是每当用户想要停止输入时,我想要到达显示“到达此处”的打印行。我想这样做是因为一旦脱离循环,我可以读取文件,然后拆分输入并对其进行操作。我记得在用 C 语言编程时,曾经有 while (input != EOF);每当用户输入 ctrl+d 或 ctrl+c 时,它就会停止正在执行的操作,然后移至下一行代码。 我怎样才能在java中做到这一点?

非常感谢。

I'm trying to implement a user input interface for a board game. I'm trying to get user input one at a time and then writing it to a file (since I need to save the list of moves made by the user). What I have so far, works well (reading input and writing it to file), however, whenever the user wants to stop inputting, the program just stops working. I.E; when you press ctrl+c, the program just ends.

Here is what I have so far, the fileName variable has been declared outside the main function

public static void main(String[] args) throws IOException {
    BufferedReader inputReader = new BufferedReader (new InputStreamReader (System.in));
    try {
        FileWriter outFile = new FileWriter (fileName);
        PrintWriter out = new PrintWriter (outFile);
        System.out.print ("Enter move: ");
        String line = inputReader.readLine();
        while (line != null) {
            System.out.print ("Enter move: ");
            out.write(inputReader.readLine());
            out.write(" ");
            }
            out.close();
    } catch (IOException e) {
        System.out.println (e.getMessage());
    }

    System.out.println ("Reached here");
}

What I'm trying to do is whenever the user wants to stop inputting, I want to get to the print line where it says "Reached here". I want to do this because once outside of the loop, I can read the file and then split the input and maniplate it. I remember whilst programming in C, there used to be while (input != EOF); where whenever the user entered ctrl+d or ctrl+c, it stops whatever it is doing and then moves onto the next line of code.
How can I do this in java?

Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏末 2024-11-13 19:09:41

如果在每次写入后刷新,那么当用户按下 control-c 时,您至少应该获得一个完整的文件。

至于在 control-c 发生后处理更多信息,您可以通过使用关闭挂钩来完成,例如:

Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run() { 
        // do something before quiting
    }
});

但是,我不知道您可以取消终止。

我会为“不再输入”操作选择更正常的字符输入。

If you flush after each write, you should at least get a complete file when the user hits control-c.

As for processing more information after control-c happens, you can do that by using a shutdown hook such as:

Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run() { 
        // do something before quiting
    }
});

however, i don't know that you can cancel the termination.

I would choose a more normal character input for the 'no more input' action.

浅唱々樱花落 2024-11-13 19:09:41

您可以使用 信号处理程序。尽管我不推荐这样做,因为这会使用户很难退出您的应用程序。相反,您可以在用户未输入任何内容时停止输入,或使用不同的命令信号。

You can catch the Ctrl+C signal using a SignalHandler. Although I wouldn't recommend this as it would make it difficult for the user to exit your application. Instead you could stop input when the user enters nothing, or use a different command signal.

や莫失莫忘 2024-11-13 19:09:41

您需要同意指示用户已完成的命令(例如“EOF”),并在 while 循环中添加以下内容:

    while (line != null) {
        System.out.print ("Enter move: ");
        String r = inputReader.readLine();
        if ( r != null ) {
           if ( "EOF".equals(r) ) break;
        }
        out.write();
        out.write(" ");
    }

You need to agree with a command indicating that user is done (for example "EOF") and add the following in your while loop:

    while (line != null) {
        System.out.print ("Enter move: ");
        String r = inputReader.readLine();
        if ( r != null ) {
           if ( "EOF".equals(r) ) break;
        }
        out.write();
        out.write(" ");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文