【问题标题】:Problems with the oputput in the console, C# [duplicate]控制台中的输出问题,C# [重复]
【发布时间】:2015-06-23 07:50:11
【问题描述】:

我正在学习用 C# 编程,但我遇到了控制台问题,当我在控制台下方运行代码时会显示输出,但立即关闭窗口,我什么也看不到。我不知道该怎么做才能保持窗户打开。有什么建议吗?我将不胜感激。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace testApp_1
{
    class Program
    {
        static void Main(string[] args)
        {
            String word = "Hello world!";

            Console.WriteLine(word);
        }
    }
}

【问题讨论】:

  • 添加Console.Readline()
  • Console.ReadKey(false);
  • Crtl + F5。在您按任意键之前,控制台窗口不会关闭。

标签: c#


【解决方案1】:

这是因为当你到达最后一行时,你也到达了程序的末尾,因此它终止了。要保持控制台窗口打开,只需添加 Console.ReadKey() 作为最后一行。

【讨论】:

    【解决方案2】:

    写入控制台后继续执行,然后程序退出。你需要做一些事情来暂停执行,通常你会从控制台读取一些东西:

    Console.ReadKey(); 
    

    暂停直到用户按键

    所以你的整个程序可能看起来像:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace testApp_1
    {
        class Program
        {
            static void Main(string[] args)
            {
                String word = "Hello world!";
    
                Console.WriteLine(word);
    
                //if you want the user to exit with any key press do this
                Console.WriteLine("Press any key to exit.");
                Console.ReadKey();
    
                //if you want the user to hit 'enter' to exit do this
                Console.WriteLine("Press enter to exit.");
                Console.ReadLine();
            }
        }
    }
    

    您可以在 MSDN 上阅读有关 Console.ReadKeyConsole.ReadLine 的信息

    【讨论】:

      【解决方案3】:

      您的代码会立即关闭,因为它没有其他事情可做。您要做的就是将其添加到代码的底部:

      Console.Readline();
      

      这将导致它等到您按 Enter 键。

      【讨论】:

        【解决方案4】:

        您可以使用以下方法读取一些用户输入: https://msdn.microsoft.com/de-de/library/system.console.readline%28v=vs.110%29.aspx

        但你必须做一些输入。

        您还可以使用记录器框架:log4net: http://logging.apache.org/log4net/

        【讨论】:

          【解决方案5】:

          Console.ReadLine() 丢失。

          【讨论】:

            【解决方案6】:

            如果你按 ctrl + F5(不调试就运行)它不会关闭。但是如果你用调试运行,它会在执行后关闭。 最后可以使用Console.ReadLine();等待用户回车。

            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;
            using System.Threading.Tasks;
            
            namespace testApp_1
            {
                class Program
                {
                    static void Main(string[] args)
                    {
                        String word = "Hello world!";
            
                        Console.WriteLine(word);
                        Console.ReadLine();
                    }
                }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-08-13
              • 2012-09-28
              • 2016-09-15
              • 2012-04-26
              • 1970-01-01
              相关资源
              最近更新 更多