C# 使用 foreach 计算数组中整数的总和

问题描述 投票:0回答:3

请帮忙。被这段代码困住了,尝试了不同的方法,但无法使其正常工作。我有一个“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();
c loops multidimensional-array foreach sum
3个回答
1
投票

我将“foreach”循环移到“for”循环之外,并修复了一个错误。这是代码:

    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;
© www.soinside.com 2019 - 2024. All rights reserved.