2016-04-24 5 views
0

私はC#クラスで書くための簡単なプログラムを持っており、最後の部分を理解することはできません。私は0から20までの偶数をすべて列に書いておき、その後に2乗値の列とキューブ値の列をもう1つ持たなければなりません。私はすべてそれを働かせている。私は最後に各列の合計が必要ですが、それを理解できないようです。Cの数値の列を取得する方法#

ご協力いただければ幸いです。私のコードは以下に含まれています。

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


    } 
} 

}

+0

ループが合計を維持するために3つの変数を追加する前に、sumSquaredとsumCubeは、ループ内の電子の値を追加し、E1、E2、ループの外でそれらを印刷 – Steve

答えて

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); 
関連する問題