【发布时间】:2009-11-15 01:18:13
【问题描述】:
我正在评估用户输入作为我的应用程序的命令。如果用户按下 Q 或 q,然后按 Enter,则应用程序退出并终止执行。
是否有适当的背景或如何做到这一点的最佳实践?我没有任何资源可以释放,或者类似的东西。我应该只使用System.exit(0); 吗?有推荐的方法吗?
作为我的第一种方法,我会这样做:
while (true){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//Other logic goes here...
if (br.readLine().equalsIgnoreCase("Q")){
System.exit(0);
}
}
catch (IOException ioe) {
System.out.println("IO error trying to read your selection");
}
}
【问题讨论】:
-
System.exit(0);没有任何问题。但我认为你有更重要的事情要担心,比如正确缩进你的代码。 -
抱歉,我匆忙复制/粘贴了
标签: java stdout user-input exit-code