C#如何访问数组中的值并比较它的值

时间:2014-06-30 21:28:20

标签: c# multidimensional-array

我正在尝试建立一个基本火星探测器程序来控制火星探测器的运动。网格区域为100米x 100米。漫游车朝南开始,可以左右移动,以米为单位,随时最多可以输入5个命令。流动站从网格位置1开始,在每组命令后报告其当前位置和方向。

e.g

  1. 50米
  2. 23米
  3. 4米
  4. 上述命令集将导致流动站报告位置4624 / North。

    此时我想将一个数组传递给一个方法来检查用户移动的行和列,以确保它们在网格边界内。

    如何访问数组中的值?

    这是我目前所拥有的,但我收到错误:

      

    无效的表达式术语'int'在int [rows,columns] = grid;

    public bool gridBoundary(int[,] grid, int choice)
    {
        int rows = 0;
        int columns = 0;
    
    
        int [rows, columns] = grid;
    
    
        if (choice > rows || choice > columns) {
            return false;
        }
    
        return true;
    }
    

2 个答案:

答案 0 :(得分:0)

如果您想获得尺寸的长度,请使用Array.GetLength方法:

int rows = grid.GetLength(0);
int columns = grid.GetLength(1);

答案 1 :(得分:0)

我使用 grid.length > = 0 来确定移动是否在网格的边界内。如果Rover在边界内,我会根据流动站最初面向的方向和使用的方向设置新的面向位置。这些都放在 For Loop 内,以产生5次移动。

         /*
         * startingDirection North 1, East 2, South 3, West 4
         * userDirection Left 1, Right 2, None 3
         *
         */ Works out the current position of the Rover

    public void currentLocation(int[,] grid, int direction,int row, int column, int move )
    {
        Scanner scanner = new Scanner();
        Messgae message = new Messgae();

        for (int i = 0; i <=5; i++)
        {
            switch (direction)
            {
                case 1:
                    if (row - move >= 0 ) 
                    {
                        Console.WriteLine("\n Direction facing North, grid position " + grid.GetValue(row - move, column) + "\n");
                        row = row - move;
                        direction = currentDirection(scanner.getDirection(), 1);
                        move = scanner.getMove();
                        break;
                    }
                    else
                    {
                        message.endMessage();
                        i = 5;
                        break;
                    }

                case 2:
                    if(column + move <= grid.Length) 
                    {
                        Console.WriteLine("\n Direction facing East, grid position " + grid.GetValue(row, column + move) + "\n");
                        direction = currentDirection(scanner.getDirection(), 2);
                        column = column + move;
                        move = scanner.getMove();
                        break;
                    }
                    else
                    {
                        message.endMessage();
                        i = 5;
                        break;
                    }

                case 3:
                    if(row + move <= grid.Length)
                    {
                        Console.WriteLine("\n Direction facing South, grid position " + grid.GetValue(row + move, column) + "\n");
                        direction = currentDirection(scanner.getDirection(), 3);
                        row = row + move;
                        move = scanner.getMove();
                        break;
                    }
                    else
                    {
                        message.endMessage();
                        i = 5;
                        break;
                    }

                case 4:
                    if (column - move >= 0)
                    {
                        Console.WriteLine("\n Direction facing West, grid position " + grid.GetValue(row, column - move) + "\n");
                        direction = currentDirection(scanner.getDirection(), 4);
                        column = column - move;
                        move = scanner.getMove();
                        break;
                    }
                    else
                    {
                        message.endMessage();
                        i = 5;
                        break;
                    }

                default:
                    break;

            }// End switch
        }// End for loop
    }// End currentLocation()