使用foreach的数组中的整数C#

时间:2015-03-02 07:17:52

标签: loops multidimensional-array foreach sum totals

请帮忙。坚持使用此代码尝试了不同的东西,但无法使其正常工作。我有一个“foreach”循环,从技术上讲,它必须对2D数组中的所有整数求和,但是,计算的答案不是预期的。我究竟做错了什么?谢谢,谢谢。

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

3 个答案:

答案 0 :(得分:1)

所以我发现了逻辑错误。我感动了&#34; foreach&#34;循环在&#34;之外&#34;循环,它修复了一个错误。感谢您的时间和支持。这是工作代码:         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();
    }
}

}

答案 1 :(得分: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;

}

答案 2 :(得分: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;