简体   繁体   English

如何在C#中获得一列数字的总和

[英]How to get the sum of a column of numbers in c#

I have a simple program to write in my c# class and I cant figure out the last part. 我有一个简单的程序可以在c#类中编写,而我无法弄清最后一部分。 I have to write out all even numbers from 0 - 20 in a column and then have another column of the square values and one more column of the cube values. 我必须在一列中写出从0到20的所有偶数,然后再有另一列平方值和另一列立方值。 I have got all that working. 我已经做了所有的工作。 I then need the sums of each column at the end, but cant seem to figure it out. 然后,我需要最后每一列的总和,但似乎无法弄清楚。

Any help would be appreciated. 任何帮助,将不胜感激。 My code is included below. 我的代码包含在下面。

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

namespace COMP2614Assign01
{
class Program
{
    static void Main(string[] args)
    {
        string formatStringHeading = "{0,5} {1, 10} {2, 10:N0}";

            Console.WriteLine(formatStringHeading
                ,"number"
                ,"square"
                ,"cube"
                 );
         Console.WriteLine(new string('-', 28));

         int e = 0;

         while (e <= 20)
         {
             int e1 = e * e;
             int e2 = e * e * e;
             Console.WriteLine(formatStringHeading
                 ,e
                 ,e1
                 ,e2);
             e += 2;
         }
         Console.WriteLine(new string('-', 28));


    }
}

} }

int e = 0;
int eSum = 0, e1Sum = 0, e2Sum = 0;

while (e <= 20)
{
    eSum += e;
    int e1 = e * e;
    e1Sum += e1;
    int e2 = e * e * e;
    e2Sum += e2;
    Console.WriteLine(formatStringHeading
        ,e
        ,e1
        ,e2);
    e += 2;
}
Console.WriteLine(new string('-', 28));
Console.WriteLine(formatStringHeading
    ,eSum
    ,e1Sum
    ,e2Sum);

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

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