无法从读取标准输入流的方法返回 main()

发布于 2024-08-11 19:38:55 字数 2225 浏览 13 评论 0原文

我基本上试图从从标准输入流读取用户输入的方法返回。由于用户可以选择退出应用程序,因此我试图找出执行此操作的最佳方法 退出。理想情况下,我将能够从 begin() 返回并让 main() 完成,从而退出应用程序。

public static void main(String[] args) {
     begin();
}

private static void begin(){
        Machine aMachine = new Machine();
        String select=null;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while(true){
            try {
                select = br.readLine();
            } catch (IOException ioe) {
                System.out.println("IO error trying to read your selection");
                return;
            }catch(Exception ex){
                System.out.println("Error trying to evaluate your input");
                return;
            }

            if (Pattern.matches("[RQrq1-6]", select)) {
                aMachine.getCommand(select.toUpperCase()).execute(aMachine);
            }
            else {                
                System.out.println(aMachine.badCommand()+select);
                aMachine.getStatus();
            }
        }
    }

aMachine使用此方法执行用户给定的命令时,主要逻辑发生:

aMachine.getCommand(select.toUpperCase()).execute(aMachine);

同样,问题是用户输入命令 Q 或 q 后如何退出应用程序。 quit 命令是这样的:

public class CommandQuit implements Command {

    public void execute(Machine aMachine) {
        aMachine.getStatus();
        return; //I would expect this to force begin() to exit and give control back to main()
    }
}

现在遵循我之前的建议 问题,要退出应用程序,我试图返回 main() 并基本上让 main() 完成。这样我就可以避免使用 System.exit(0),尽管那也很好。

因此,在这个示例中,我在 CommandQuit 类的 execute 方法中有一个 return 语句,当我们收到 Q 时会调用该语句,或者q 来自用户。但是,当 begin() 执行退出命令时,不是从 while(true) 循环返回,而是退出 begin(),并且回到 main(),控制流似乎永远不会响应 CommandQuit 的 execute 方法中的 return;

我的示例中是否缺少任何内容?也许有些事情太明显了,以至于我现在看不到它。感谢您的任何帮助。

I am basically trying to return from a method which reads user input from the standard input stream. Since the user has the option to quit the application, I am trying to figure out the best way to do this exit. Ideally I will be able to return from begin() and let main() finish, thus quiting the applicaiton.

public static void main(String[] args) {
     begin();
}

private static void begin(){
        Machine aMachine = new Machine();
        String select=null;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while(true){
            try {
                select = br.readLine();
            } catch (IOException ioe) {
                System.out.println("IO error trying to read your selection");
                return;
            }catch(Exception ex){
                System.out.println("Error trying to evaluate your input");
                return;
            }

            if (Pattern.matches("[RQrq1-6]", select)) {
                aMachine.getCommand(select.toUpperCase()).execute(aMachine);
            }
            else {                
                System.out.println(aMachine.badCommand()+select);
                aMachine.getStatus();
            }
        }
    }

The main logic takes place when aMachine executes a given command by the user with this method:

aMachine.getCommand(select.toUpperCase()).execute(aMachine);

Again, the issue is how to quit the application once the user enters the command Q, or q. The quit command is like this:

public class CommandQuit implements Command {

    public void execute(Machine aMachine) {
        aMachine.getStatus();
        return; //I would expect this to force begin() to exit and give control back to main()
    }
}

Now following the advice from my previous question, to quit the application, I am trying to return to main() and basically let main() complete. This way I avoid any use of System.exit(0), although that would be fine too.

So, in this example, I have a return statement in the execute method of the CommandQuit class which is called when we receive a Q, or q from the user. However, when begin() executes a quit command, instead of returning from the while(true) loop, out of begin(), and back into main(), the control flow never seems to respond to the return; within the execute method of CommandQuit.

Is there anything that I am missing in my example? Perhaps something is so obvious that I can't see it at the moment. Thanks for any help.

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

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

发布评论

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

评论(2

难忘№最初的完美 2024-08-18 19:38:55

execute() 中的 return 语句从 execute() 返回,而不是 begin()。通常在这些情况下,CommandQuit.execute()aMachine 上设置一个标志,然后 begin() 检查 aMachine 顶部的标志。环形:

while (aMachine.stillRunning()) {  // instead of while (true)
    ...
    // This will clear aMachine.stillRunning() if the user quits.
    aMachine.getCommand(select.toUpperCase()).execute(aMachine);
}

The return statement in execute() returns from execute(), not begin(). Usually in these cases, CommandQuit.execute() sets a flag on aMachine, and then begin() checks the flag at the top of the loop:

while (aMachine.stillRunning()) {  // instead of while (true)
    ...
    // This will clear aMachine.stillRunning() if the user quits.
    aMachine.getCommand(select.toUpperCase()).execute(aMachine);
}
傲性难收 2024-08-18 19:38:55
return;

始终仅从当前函数返回(在本例中为 CommandQuit.execute)。这与在函数末尾执行完全相同。您可以在 aMachine 中设置一个标志,并让 while 循环运行直到它被设置,而不是永远运行:

在“Machine”类中,您将:

    private boolean isRunning = false;
    public boolean stillRunning() {
        return isRunning;
    }
    public void stopRunning() {
        isRunning = false;
    }

并且您将循环 while aMachine.stillRunning() 并调用 aMachine.stopRunning() 来停止它。

return;

always returns from the current function only (in this case CommandQuit.execute). It's exactly the same as executing past the end of the function. You could set a flag in aMachine, and make the while loop run until it's set instead of running forever:

in the class "Machine" you would have:

    private boolean isRunning = false;
    public boolean stillRunning() {
        return isRunning;
    }
    public void stopRunning() {
        isRunning = false;
    }

and you would loop while aMachine.stillRunning() and call aMachine.stopRunning() to stop it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文