Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么控制台在我上次输入后关闭?_C#_Console - Fatal编程技术网

C# 为什么控制台在我上次输入后关闭?

C# 为什么控制台在我上次输入后关闭?,c#,console,C#,Console,我的程序允许用户输入20个价格,并显示这些价格的平均值。为什么在我输入最后一次输入后控制台会关闭?下面是我正在运行的代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace machineproblem4 { class Program { static void Main(string[] args) {

我的程序允许用户输入20个价格,并显示这些价格的平均值。为什么在我输入最后一次输入后控制台会关闭?下面是我正在运行的代码:

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

namespace machineproblem4
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 0;
            double average = 0;

            Console.WriteLine("\t\t\t         INPUT PRICES  \n");
            int[] price = new int[20];
            Console.WriteLine("\t\t\t  Please enter 20 prices \n");

            for (int ctr = 0; ctr < 20; ctr++)
            {
                Console.Write("Enter price {0} :   ", ctr + 1);
                price[ctr] = Convert.ToInt32(Console.ReadLine());

            }

            // [...calculate sum...]

            //average 

            Console.WriteLine("\n----------------------------");
            Console.WriteLine("||average of the prices||");
            average = sum / 20;
            Console.WriteLine("average of the prices:   {0}", average);

            //more code that outputs statistics about the inputs

            //exit

            //Edit: This is what fixed my problem
            Console.WriteLine("press any key to exit ..");
            Console.ReadKey();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间机器问题4
{
班级计划
{
静态void Main(字符串[]参数)
{
整数和=0;
双平均=0;
Console.WriteLine(“\t\t\t输入价格\n”);
整数[]价格=新整数[20];
Console.WriteLine(“\t\t\t请输入20个价格\n”);
对于(int ctr=0;ctr<20;ctr++)
{
写入(“输入价格{0}:”,ctr+1);
price[ctr]=Convert.ToInt32(Console.ReadLine());
}
//[…计算总和…]
//平均值
Console.WriteLine(“\n-------------------------------------------”);
Console.WriteLine(“平均价格”);
平均值=总和/20;
Console.WriteLine(“价格的平均值:{0}”,平均值);
//更多输出有关输入的统计信息的代码
//出口
//编辑:这就是解决我问题的方法
Console.WriteLine(“按任意键退出…”);
Console.ReadKey();
}
}
}

您可以在最后一条语句处放置一个Console.Read()。您还可以在最后一条语句处放置断点,放置:

Console.Readline();

在您的主函数结束时,它将等待您按enter键后关闭。

使用
Console.Readline()

Read()、ReadLine()和ReadKey()基本上是静态方法,它们属于Console类。这就是为什么我们使用以下方法:

Console.Read():
--方法接受字符串并返回整数

Console.ReadLine()
:--方法接受字符串并返回字符串

Console.ReadKey()
:--方法接受字符并返回字符

这就是为什么我们主要使用Console.ReadKey()方法,从输出窗口返回源代码

因为当我们只按下字符时,我们直接进入源代码。如果要使用Console.Read()和Console.ReadLine方法,则
您需要按Enter键,返回源代码,而不是任何字符。

通常,等待控制台应用程序的用户输入不是一个好主意。这对于调试来说是可以的,但对于发布来说不是绝对可以的。 因此,首先使用

private static bool IsDebug()
{
    object[] customAttributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(DebuggableAttribute), false);
    if ((customAttributes != null) && (customAttributes.Length == 1))
    {
        DebuggableAttribute attribute = customAttributes[0] as DebuggableAttribute;
        return (attribute.IsJITOptimizerDisabled && attribute.IsJITTrackingEnabled);
    }
    return false;
}
然后用,

if (IsDebug())
    Console.Readline();

这样就不需要为不同的构建配置编辑代码。另一种方法是设置断点并调试控制台应用程序,正如@Erwin所建议的那样,前面的答案实际上都没有直接回答为什么会发生这种情况的问题。控制台在上次输入后关闭的原因是,其余代码运行得非常快,当它到达程序末尾时,控制台关闭。这是正确的行为,应该在运行控制台应用程序时出现。正如其他答案所述,您可以通过在关闭控制台之前要求最终输入来解决这一问题,但这只是一个解决办法

如果要输出到文本文件,而不仅仅是控制台,则会看到所有输出都是按预期生成的。控制台输出和关闭的速度太快,您无法在代码中看到它,而不会出现某种停顿


此外,一个尚未提及的解决方案是在不进行调试的情况下从Visual Studio运行项目,在关闭控制台之前完成处理后,将自动输出“按任意键继续…”。这样,您就可以看到它输出了什么,而不需要在生产代码中包含您不希望包含的无关代码。

Where??更具体地说,这种措辞有些无益。您真正的问题是“为什么在我输入最后一个输入后控制台会关闭?”,但问题主体中唯一的问题是,“我是否遗漏了什么?”我将对其进行编辑以澄清问题