使控制台等待用户输入关闭 [英] Make the console wait for a user input to close

查看:139
本文介绍了使控制台等待用户输入关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制台应用程序,在执行任务后,必须向用户提供反馈,例如操作已完成或操作失败以及详细错误。

I have a console application that after performing its tasks, must give feedback to the user, such as "operation completed" or "operation failed" and the detailed error.

问题是,如果我只是让它运行,输出消息将被打印,但控制台将很快关闭,没有时间阅读消息。

The thing is, if I just "let it run", the output message will be printed but the console will close shortly afterwards, leaving no time to read the message.

据我记忆,在C ++中,每个控制台应用程序都将以按任意键退出或类似的结尾。在C#中,我可以用

As far as I remember, in C++, every console application will end with a "press any key to exit" or something like that. In C# I can simulate this behavior with a

Console.ReadKey();

但我怎么能用Java做呢?我正在使用Scanner类,但鉴于输入是我的Scanner实例:

But how can I do it in Java? I'm using the Scanner class, but given that "input" is my instance of Scanner:

input.next()
System.exit(0);

任何关键都可以使用,除了退货,这在这里非常重要。任何指针?

"Any key" will work, except for return, which is quite a big deal here. Any pointers?

推荐答案

在Java中,这将是 System.in.read()

In Java this would be System.in.read()

这篇关于使控制台等待用户输入关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆