首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >减去对象的数组值

减去对象的数组值
EN

Stack Overflow用户
提问于 2014-03-25 06:17:37
回答 1查看 204关注 0票数 1

我如何从细胞5,4减去细胞3,5?我有一个单元对象,它创建一个2D数组,并在其中设置战士对象。我试过了,但没有用

代码语言:javascript
复制
Cell[,] cells = new Cell[6,6];
cells[3, 5] = new Cell(warrior);
cells[5,4] = new Cell(warrior);

...

int x1 = cells[3, 5].x - cells[5, 4].x;
int x2 = cells[3, 5].y - cells[5, 4].y;
Console.WriteLine(x1);
Console.WriteLine(x2);

我的细胞类是这样的:

公共类单元{ public int _x;public int _y;public Warrior _warrior;}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-25 06:35:46

我已经编写了一个示例代码,试着这样做..

代码语言:javascript
复制
 Warrior warrior = new Warrior(25,24);
        Warrior warrior1 = new Warrior(20,20);

        Cell[,] cells = new Cell[6, 6];
        cells[3, 5] = new Cell(warrior);
        cells[5, 4] = new Cell(warrior1);

        int x1 = cells[3, 5]._x - cells[5, 4]._x;
        int x2 = cells[3, 5]._y - cells[5, 4]._y;
        Console.WriteLine(x1);
        Console.WriteLine(x2);

 public class Cell
{
    public Cell(Warrior warrior)
    {
        _x = warrior.x;
        _y = warrior.y;
    }

    public int _x;
    public int _y;
    public Warrior _warrior;
}

public class Warrior
{
    public Warrior(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public int x;

    public int y;
}

OutPut :5 4

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22626783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档