如何在C#中获取用户输入的数组的和和乘法

问题描述 投票:-1回答:1

我编写了这段代码来从用户那里获取数组值并显示它们。

namespace Program_2
{

    class Program
    {
        static void Main(string[] args)
        {
            int[,] nums = new int[6,2];

            for (int x = 0; x < 5; x++)
            {
                for (int y = 0; y < 2; y++)
                {
                    Console.WriteLine("Enter Numbers");
                    nums[x, y] = int.Parse(Console.ReadLine());
                }
            }

            for(int j=0; j < 5; j++)
            {
                for(int k = 0; k < 2; k++)
                {
                    Console.Write(nums[j, k]+" ");
                }
                Console.WriteLine("");
            }

            Console.ReadLine();
        }
    }
}

但我想知道如何进行用户输入的数组的乘法和加法。

喜欢这个"See this image"

c# visual-studio console-application
1个回答
0
投票

int total = 0;
        // Iterate first dimension of the array
        for (int i = 0; i < nums.GetLength(0); i++)
        {
            // Iterate second dimension
            for (int j = 0; j < nums.GetLength(1); j++)
            {
                total += array[i, j];
            }
        }

Console.WriteLine(total);

MULTIFICATION

int total = 0;
        // Iterate first dimension of the array
        for (int i = 0; i < nums.GetLength(0); i++)
        {
            // Iterate second dimension
            for (int j = 0; j < nums.GetLength(1); j++)
            {
                total *= array[i, j];
            }
        }

Console.WriteLine(total);

两个一个

int sum= 0;
int mul= 0;
            // Iterate first dimension of the array
            for (int i = 0; i < nums.GetLength(0); i++)
            {
                // Iterate second dimension
                for (int j = 0; j < nums.GetLength(1); j++)
                {
                    sum += array[i, j];
                    mul *= array[i, j];
                }
            }

    Console.WriteLine("SUM = " + sum);
    Console.WriteLine("MUL = " + mul);
© www.soinside.com 2019 - 2024. All rights reserved.