无法从读取标准输入流的方法返回 main()
我基本上试图从从标准输入流读取用户输入的方法返回。由于用户可以选择退出应用程序,因此我试图找出执行此操作的最佳方法 退出。理想情况下,我将能够从 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
execute()
中的 return 语句从execute()
返回,而不是begin()
。通常在这些情况下,CommandQuit.execute()
在aMachine
上设置一个标志,然后begin()
检查 aMachine 顶部的标志。环形:The return statement in
execute()
returns fromexecute()
, notbegin()
. Usually in these cases,CommandQuit.execute()
sets a flag onaMachine
, and thenbegin()
checks the flag at the top of the loop:始终仅从当前函数返回(在本例中为 CommandQuit.execute)。这与在函数末尾执行完全相同。您可以在 aMachine 中设置一个标志,并让 while 循环运行直到它被设置,而不是永远运行:
在“Machine”类中,您将:
并且您将循环 while aMachine.stillRunning() 并调用 aMachine.stopRunning() 来停止它。
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:
and you would loop while aMachine.stillRunning() and call aMachine.stopRunning() to stop it.