简体   繁体   English

为什么我的控制台应用程序立即消失

[英]Why does my console application disappear immediately

I've made a Console Application is Microsoft Visual Studio 2012. This is what i've added to my code. 我制作的控制台应用程序是Microsoft Visual Studio 2012.这是我添加到我的代码中的内容。

    static void Main(string[] args)
    {
        Console.WriteLine("Hello World");
    }

When I'm debugging this program, the program close immediatly! 当我调试这个程序时,程序立即关闭! What am I doing wrong? 我究竟做错了什么?

您必须将Console.ReadLine()添加到Main方法的末尾,这将使控制台等待,直到用户将任何输入发送到控制台,然后它将退出。

Al the existing answers are correct, but Ctrl-F5 (instead of just F5 in VS) to run will also have the effect of waiting for a keystroke. 虽然现有答案是正确的,但运行中的Ctrl-F5(而不仅仅是VS中的F5)也会产生等待击键的效果。

EDIT 编辑

The above will not allow you to debug as such, but you don't have a breakpoint in there anyway. 以上内容不允许您这样调试 ,但是无论如何都没有断点。 Pressing F10 will step through the program line by line. 按F10将逐行逐步执行程序。

you have to write the 你必须写

Console.WriteLine("Hello World");
Console.ReadLine();

Because the only statement you have just prints a line and the execution of program is finished and program is exited. 因为你只有一个语句打印一行,程序的执行完成,程序退出。 Use Console.ReadLine Line to stop window to get disappear. 使用Console.ReadLine Line停止窗口消失。 Use read line will exit program when Enter key is pressed. 当按下Enter键时,使用read line将退出程序。

You can use ReadKey if you want to do like "Press any key to continue..." The will cause the program to exit when any key is pressed. 如果你想这样做,你可以使用ReadKey “按任意键继续...”当按下任何键时,将导致程序退出。

static void Main(string[] args)
{
    Console.WriteLine("Hello World");
    Console.Read();
}

You should add Console.ReadLine(); 你应该添加Console.ReadLine(); after WriteLine method, So your application will wait user to input. WriteLine方法之后,所以你的应用程序会等待用户输入。 After user pressed any keys, Your application will disappear. 用户按任意键后,您的应用程序将消失。

   static void Main(string[] args)
    {
        Console.WriteLine("Hello World");
        Console.ReadLine();
    }

You could just add 你可以添加

Console.ReadKey();

This will await a keystroke before closing the program. 这将在关闭程序之前等待击键。

all of the above is correct. 所有这些都是正确的。 Typically, Console.ReadKey() is used for control purpose instead logic processing. 通常,Console.ReadKey()用于控制目的而不是逻辑处理。

static void Main(string[] args)
{
    Console.WriteLine("Hello World");
    string line = Console.ReadKey();
}

you can get more information here 你可以在这里获得更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM