2015-03-02 327 views
0

請幫忙。被這個代碼困住了,嘗試了不同的東西,但無法使它正常工作。我有一個「foreach」循環,在技術上它必須將二維數組中的所有整數相加,然而,計算出的答案並不是預期的結果。我究竟做錯了什麼?謝謝謝謝。C#數組中使用foreach的整數求和

static void Main(string[] args) 
    { 

     const int ROWS = 2; 
     const int COLS = 2; 
     const int MAX = 5; 

     string input; 
     int[,] numbers = new int[ROWS, COLS]; 


     do 
     { 
      int total = 0; 
      double avg = 0; 
      Random rand = new Random(); 
      for (int rows = 0; rows < ROWS; ++rows) 
      { 
       for (int cols = 0; cols < COLS; ++cols) 
       { 
        numbers[rows, cols] = rand.Next(1, MAX); 
       } 
       { 
        for (int cols = 0; cols < COLS; ++cols) 
         Console.Write(" {0, 3}", numbers[rows, cols]); 
        Console.WriteLine(); 
       } 

    foreach (int cell in numbers) 
       { 
        total += cell; 
       } 

       avg = total/4.0; 

      } Console.WriteLine("Sum: {0:0,0} Average: {1:f}", total, avg); 

      Console.Write("\nWould you like to generate a new table? Type yes or no... "); 
      input = Console.ReadLine().ToLower(); 

      if (input == "no") 
      { 
       Console.WriteLine("End of program. Press any key to exit. Goodbye."); 
      } 
     } 

     while (input == "yes"); 






     Console.ReadKey(); 

回答

0

我認爲你的foreach是在這裏重複:

for (int rows = 0; rows < ROWS; ++rows) 
{ 
    for (int cols = 0; cols < COLS; ++cols) 
    { 
     numbers[rows, cols] = rand.Next(1, MAX); 
     total += numbers[rows, cols]; // Sum in the same loop 
    } 

    for (int cols = 0; cols < COLS; ++cols) 
     Console.Write(" {0, 3}", numbers[rows, cols]); 
    Console.WriteLine(); 

    avg = total/4.0; 

} 
0

我同意@QtRoS你的週期是多餘的 - 你總結了所有單元格的值的兩倍(每行)。

但我認爲在代碼中還有一個錯誤(在@ QtRoS的答案中也是如此)。 如果要計算所有網格單元的平均值,則必須在行循環後執行此操作。

這樣:

for (int rows = 0; rows < ROWS; ++rows) 
{ 
    for (int cols = 0; cols < COLS; ++cols) 
    { 
     numbers[rows, cols] = rand.Next(1, MAX); 
     total += numbers[rows, cols]; // Sum in the same loop 
    } 

    for (int cols = 0; cols < COLS; ++cols) 
     Console.Write(" {0, 3}", numbers[rows, cols]); 
    Console.WriteLine(); 
} 

avg = total/4.0; 
1

所以我找到了邏輯錯誤。我將「foreach」循環移到了「for」循環之外,並修復了一個錯誤。感謝您的時間和支持。這裏的工作代碼: 靜態無效的主要(字串[] args){

 const int ROWS = 2; 
     const int COLS = 2; 
     const int MAX = 5; 

     string input; 
     int[,] numbers = new int[ROWS, COLS]; 


     do 
     { 
      int total = 0; 
      double avg = 0; 
      Random rand = new Random(); 
      for (int rows = 0; rows < ROWS; ++rows) 
      { 
       for (int cols = 0; cols < COLS; ++cols) 
       { 
        numbers[rows, cols] = rand.Next(1, MAX); 
       } 
       { 
        for (int cols = 0; cols < COLS; ++cols) 
         Console.Write(" {0, 3}", numbers[rows, cols]); 
        Console.WriteLine(); 
       } 

      } 


       foreach (int cell in numbers) 
       { 
        total += cell; 
       } 

       avg = total/4.0; 
      Console.WriteLine("Sum: {0:0,0} Average: {1:f}", total, avg); 

      Console.Write("\nWould you like to generate a new table? Type yes or no... "); 
      input = Console.ReadLine().ToLower(); 

      if (input == "no") 
      { 
       Console.WriteLine("End of program. Press any key to exit. Goodbye."); 
      } 
     } 

     while (input == "yes"); 






     Console.ReadKey(); 
    } 
} 

}