我想获得下面的代码来打印出“总和”到控制台,它编译罚款和一切,但从来没有打印出总和。有人可以给我一个提示吗?谢谢。C#:Console.WriteLine不打印出结果
class Program
{
static void Main(string[] args)
{
// Not sure how to call "ArraySum" from here.
}
public void ArraySum()
{
int[] arr = { 1, 2, 3, 4, 5 };
int sum = 0;
foreach (int x in arr)
{
sum += x;
}
Console.WriteLine(sum);
}
编辑:抱歉没有发布整个代码,但是。基本上我不知道如何从main调用该方法并将其打印出来。
你有没有打过'ArraySum'? – Carcigenicate
我认为重复是错误的。他没有说在他看到它之前就关闭了,他说它只是不打印。 – Carcigenicate
您可能不会从'main()'中调用此方法,或者在您有机会读取输出之前控制台窗口会消失。后者请参阅[重复](http://stackoverflow.com/questions/8868338/why-is-the-console-window-closing-immediately-without-displaying-my-output)。如果不适用,请阅读[问]并提供[mcve],其中包含足够的代码以正确显示问题。 – CodeCaster