Java 中的读取键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2533987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Readkey in Java
提问by mouthpiec
I am new to Java ... is there a similar method to the ReadKey() in C# to avoid that a console application closes?
我是 Java 新手……是否有与 C# 中的 ReadKey() 类似的方法来避免控制台应用程序关闭?
thanks
谢谢
采纳答案by Chris Lercher
One way to do it:
一种方法:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
br.readLine();
there are definitely shorter ones, but I think this one's convenient, because it can easily be extended.
肯定有更短的,但我认为这个很方便,因为它可以很容易地扩展。
回答by Antony Thomas
YOu can also use System.in.read()
, if you are ok with just 'Enter' key being your exit key.
您也可以使用System.in.read()
,如果您可以将“Enter”键作为退出键。