【问题标题】:Finding the sum of the values in a 2D Array in C#在 C# 中查找二维数组中的值的总和
【发布时间】:2015-12-13 17:48:32
【问题描述】:

尝试构建一个方法来查找二维数组中所有值的总和。我对编程很陌生,找不到一个好的起点来试图弄清楚它是如何完成的。这是我到目前为止所拥有的(请原谅我,我通常是一个英语/历史专家,逻辑不是我的强项......)

class Program
{
    static void Main(string[] args)
    {
        int[,] myArray = new int[5,6];
        FillArray(myArray);
        LargestValue(myArray);

    }
    //Fills the array with random values 1-15
    public static void FillArray(int[,] array)
    {
        Random rnd = new Random();
        int[,] tempArray = new int[,] { };
        for (int i = 0; i < tempArray.GetLength(0); i++)
        {
            for (int j = 0; j < tempArray.GetLength(1); j++)
            {
                tempArray[i, j] = rnd.Next(1, 16);
            }
        }
    }
    //finds the largest value in the array (using an IEnumerator someone 
    //showed me, but I'm a little fuzzy on how it works...)
    public static void LargestValue(int[,] array)
    {
        FillArray(array);
        IEnumerable<int> allValues = array.Cast<int>();
        int max = allValues.Max();
        Console.WriteLine("Max value: " + max);
    }
    //finds the sum of all values
    public int SumArray(int[,] array)
    {
        FillArray(array);
    }
}

我想我可以尝试找到每一行或每一列的总和,然后用 for 循环将它们相加?将它们相加并返回一个 int?如果有人能提供任何见解,将不胜感激,谢谢!

【问题讨论】:

    标签: c# multidimensional-array methods


    【解决方案1】:

    首先,您不需要在每个方法的开头调用 FillArray,您已经在 main 方法中填充了数组,您正在将填充的数组传递给这些其他方法。

    类似于你用来填充数组的循环是最容易理解的:

    //finds the sum of all values
    public int SumArray(int[,] array)
    {
        int total = 0;
        // Iterate through the first dimension of the array
        for (int i = 0; i < array.GetLength(0); i++)
        {
            // Iterate through the second dimension
            for (int j = 0; j < array.GetLength(1); j++)
            {
                // Add the value at this location to the total
                // (+= is shorthand for saying total = total + <something>)
                total += array[i, j];
            }
        }
        return total;
    }
    

    【讨论】:

    • 感谢伊恩的帮助。我特别感谢你解释得如此透彻和清晰。我一直在寻找一个地方来学习对我来说有意义的所有这些方面遇到了很多麻烦,而且你们都非常乐于助人且易于理解。
    • 如果你还在我有一个问题,我该如何调用该方法?我需要创建一个像'Object obj = new Object();'这样的新对象吗然后将其称为“int getTotal = obj.SumArray(myArray);”?我似乎找不到不涉及制作另一个对象或类的解决方案。
    • 在同一个类中,您不需要指定类/对象名称。这是因为它们在同一个 范围 内(基本上它意味着在同一组 {花括号} 内的所有内容)。通常从你的类之外,你需要像这样实例化它(创建一个实例):Program p = new Program(); p.SumArray(myArray); 但是你使用了关键字 static 这意味着即使没有类的实例也可以访问它,喜欢Program.SumArray(myArray); 研究关键字instancescopestatic 以了解更多信息。
    【解决方案2】:

    如果你知道数组的长度很容易求和 作为包含获得最高价值的奖励代码。 您可以轻松扩展它以获取其他类型的统计代码。 我假设下面的 Xlength 和 Ylength 也是整数,并且你知道。 您也可以将它们替换为代码中的数字。

    int total = 0;
    int max=0;
    int t=0;  // temp valeu
    For (int x=0;x<Xlength;x++)
    {
     for (int y=0;y<Ylength;y++)
     {
       t = yourArray[x,y]; 
       total =total +t;
       if(t>max){max=t;}   // an if on a single line
     }
    }
    

    这里是 MSDN 示例的链接,其中介绍了如何检索未知数组长度。 当您开始使用 c# 时,有一个不错的网站可供使用 谷歌“.net perls”

    【讨论】:

      猜你喜欢
      • 2013-09-13
      • 2013-03-16
      • 2022-01-02
      • 1970-01-01
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-05
      相关资源
      最近更新 更多