如何在c#中得到一列数字的总和

时间:2016-04-24 19:42:26

标签: c#

我有一个简单的程序要在我的c#类中编写,我无法弄清楚最后一部分。我必须在列中写出0到20之间的所有偶数,然后使用另一列方形值和另一列多维数据集值。我已经完成了所有工作。然后我需要最后每列的总和,但似乎无法弄明白。

任何帮助将不胜感激。我的代码包含在下面。

 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));


    }
}

}

1 个答案:

答案 0 :(得分:0)

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);