Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 二维阵列中选定元素的总和(长方体形状?)_C#_Arrays_Loops_Multidimensional Array_Sum - Fatal编程技术网

C# 二维阵列中选定元素的总和(长方体形状?)

C# 二维阵列中选定元素的总和(长方体形状?),c#,arrays,loops,multidimensional-array,sum,C#,Arrays,Loops,Multidimensional Array,Sum,我目前正在创建一个2d数组来表示网格上的元素,以便在游戏中进行布局。我希望能够对一个特定的元素块求和,但我似乎无法使我的函数正常工作,我怀疑有一种更简单的方法可以做到这一点 阵列示例: int[6,6] myArray = {{2,1,4,3,1,2,5} ,{4,2,3,3,1,2,4} ,{3,4,9,1,2,7,5} ,{1,6,2,1,3,4,2} ,{2,1,

我目前正在创建一个2d数组来表示网格上的元素,以便在游戏中进行布局。我希望能够对一个特定的元素块求和,但我似乎无法使我的函数正常工作,我怀疑有一种更简单的方法可以做到这一点

阵列示例:

int[6,6] myArray = {{2,1,4,3,1,2,5}
                ,{4,2,3,3,1,2,4}
                ,{3,4,9,1,2,7,5}
                ,{1,6,2,1,3,4,2}
                ,{2,1,4,6,2,1,0}
                ,{6,2,8,1,6,5,7}
                ,{7,6,10,3,9,7,2}};
当前代码:

int BoxSum(int x, int y, int x1, int y1,  int [,] arrayM) {
        int Results = 0; 
        int xmod = (x1 - x) / Mathf.Abs (x1 - x);
        int ymod = (y1 - y) / Mathf.Abs (y1 - y);
        if (x1 > x & y1 > y) {
            for (int ix = x; ix <= x1; ix ++) {
                for (int iy = y; iy <= y1; iy++) {
                    Results += arrayM [ix, iy];
                }
            }
        }
        if (x1 > x & y1 < y) {
            for (int ix = x; ix <= x1; ix ++) {
                for (int iy = y; iy <= y1; iy--) {
                    Results += arrayM [ix, iy];
                }
            }
        }
        if (x1 < x & y1 < y) {
            for (int ix = x; ix <= x1; ix --) {
                for (int iy = y; iy <= y1; iy--) {
                    Results += arrayM [ix, iy];
                }
            }
        }
        if (x1 < x & y1 > y) {
            for (int ix = x; ix <= x1; ix ++) {
                for (int iy = y; iy <= y1; iy--) {
                    Results += arrayM [ix, iy];
                }
            }
        }
        return Results;
    }

我希望两个都得到86(独立于我是在命令中首先输入x,y还是x1,y1)。现在他们给了我不同的结果,86和0


谢谢你的帮助

如果我读对了你的问题,你只需要一个循环嵌套,因此: 编辑:忘记了排序开始和停止位置的位!补充说。 还采纳了phoogs关于使用math min和max的建议

using System;
public class boxes{
public int[6,6] myArray;
public void boxes(){ // This is just a constructor. 
myArray = {{2,1,4,3,1,2,5}
                ,{4,2,3,3,1,2,4}
                ,{3,4,9,1,2,7,5}
                ,{1,6,2,1,3,4,2}
                ,{2,1,4,6,2,1,0}
                ,{6,2,8,1,6,5,7}
                ,{7,6,10,3,9,7,2}};
}
public int BoxSum(int x, int y, int x1, int y1) {
int sum = 0;
int xstart = Math.Min(x1, x);
int ystart = Math.Min(y1, y);
int xend = Math.Max(x1, x);
int yend = Math.Max(y1, y);
for(int i=xstart;i<=xend;i++)
{
    for(int j=ystart;j<=yend;j++)
    {
        sum += myArray[i,j];
    }
}
return sum;
}
}
使用系统;
公共类信箱{
公共int[6,6]myArray;
public void box(){//这只是一个构造函数。
myArray={2,1,4,3,1,2,5}
,{4,2,3,3,1,2,4}
,{3,4,9,1,2,7,5}
,{1,6,2,1,3,4,2}
,{2,1,4,6,2,1,0}
,{6,2,8,1,6,5,7}
,{7,6,10,3,9,7,2}};
}
公共整数盒和(整数x,整数y,整数x1,整数y1){
整数和=0;
int xstart=Math.Min(x1,x);
int ystart=数学最小值(y1,y);
int xend=数学最大值(x1,x);
int yend=数学最大值(y1,y);

对于(int i=xstart;i如果希望它工作,而不管哪些参数是高/低值,可以执行以下操作

    private int BoxSum(int x, int y, int x1, int y1, int[,] arrayM)
    {
        int lowestX = (x1 > x) ? x : x1;
        int lowestY = (y1 > y) ? y : y1;

        int highestX = (x1 > x) ? x1 : x;
        int highestY = (y1 > y) ? y1 : y;

        int sum = 0;

        for (int i = lowestX; i < highestX; i++)
        {
            for (int j = lowestY; j < highestY; j++)
            {
                sum += arrayM[i, j];
            }
        }

        return sum;
    }
private int-BoxSum(int-x,int-y,int-x1,int-y1,int[,]arrayM)
{
int lowestX=(x1>x)?x:x1;
int-lowestY=(y1>y)?y:y1;
int-highestX=(x1>x)?x1:x;
int-highestY=(y1>y)?y1:y;
整数和=0;
对于(int i=lowerstx;i
现有答案中没有一个可以解释为什么在一个案例中得到错误的结果。问题是处理该案例的块中有一个bug:

if (x1 < x & y1 < y) {
    for (int ix = x; ix <= x1; ix --) {
        for (int iy = y; iy <= y1; iy--) {
            Results += arrayM [ix, iy];
        }
    }
}
if(x1

这是一个很好的例子,说明了为什么原则很重要。省去你的头疼,按照其他答案的建议去做。

好答案。我会使用Math.Min和Math.Max。效果很好!谢谢!这是一个好答案,但我认为如果你使用Math.Min和Math.Max而不是那些条件运算符,代码的意图会更容易理解。
    private int BoxSum(int x, int y, int x1, int y1, int[,] arrayM)
    {
        int lowestX = (x1 > x) ? x : x1;
        int lowestY = (y1 > y) ? y : y1;

        int highestX = (x1 > x) ? x1 : x;
        int highestY = (y1 > y) ? y1 : y;

        int sum = 0;

        for (int i = lowestX; i < highestX; i++)
        {
            for (int j = lowestY; j < highestY; j++)
            {
                sum += arrayM[i, j];
            }
        }

        return sum;
    }
if (x1 < x & y1 < y) {
    for (int ix = x; ix <= x1; ix --) {
        for (int iy = y; iy <= y1; iy--) {
            Results += arrayM [ix, iy];
        }
    }
}
if (x1 < x & y1 < y) {
    for (int ix = x; ix >= x1; ix --) {
        for (int iy = y; iy >= y1; iy--) {
            Results += arrayM [ix, iy];
        }
    }
}