【问题标题】:C# console app closes itself after I run it [duplicate]C#控制台应用程序在我运行后自行关闭[重复]
【发布时间】:2018-05-22 22:00:12
【问题描述】:

当我运行这个简单的应用程序时,它会无缘无故地自行关闭。如何预防?

代码:

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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 10;
            int b = 20;
            int c = a * b;

            Console.WriteLine(c);
        }
    }
}

【问题讨论】:

  • Console.ReadLine();放在最后
  • 我猜你的意思是当你调试它时它会自行关闭。在末尾添加Console.ReadLine()
  • 我已经写好了答案。
  • 您需要众所周知的“按任意键继续”,这样您就可以在控制台关闭之前查看它。但仅当您在调试器中从桌面快捷方式或资源管理器中运行它时。而不是当您从命令行处理器运行它时,控制台模式应用程序的设计目的是什么。使用this solution 支持任一方式。
  • 感谢您的回答!你能点赞回答吗,因为我没有足够的积分?

标签: c# console-application


【解决方案1】:

打印值后添加Console.ReadKey();即可。

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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 10;
            int b = 20;
            int c = a * b;

            Console.WriteLine(c);
            Console.ReadKey();
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多