在方法中显示二维数组

时间:2017-01-08 13:29:49

标签: c# arrays

我正在学习如何编程,我之前使用过其他语言主要用于网络,但至少对我来说c#有点难以理解,而msdn网站是如此复杂或者我不知道正确使用它。试图构建解决数独游戏的程序,当我尝试在方法中显示二维数组时遇到问题,得到2个错误:

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        int[,] board = new int[9, 9] {{7, 2, 5, 8, 0, 4, 9, 1, 3},
                                      {0, 0, 0, 0, 3, 0, 0, 0, 4},
                                      {0, 0, 0, 0, 0, 1, 8, 2, 6},
                                      {0, 0, 9, 0, 0, 0, 0, 0, 7},
                                      {0, 1, 0, 6, 2, 8, 0, 4, 0},
                                      {2, 0, 0, 0, 0, 0, 5, 0, 0},
                                      {3, 6, 8, 4, 0, 0, 0, 0, 0},
                                      {0, 0, 0, 0, 7, 0, 0, 0, 8},
                                      {0, 0, 0, 0, 0, 2, 6, 9, 5}};

        DisplayBoard(board);

        List<int> missing = new List<int>();
        List<int> present = new List<int>();
        int temp = 0;

        for (int i = 0; i < 1; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                //Console.Write(board[i, j]);
                if (board[i, j] == 0)
                {
                    missing.Add(j);
                }
                else
                {
                    present.Add(board[i, j]);
                }

                if (present.Count == 8)
                {
                    for (int x = 0; x < present.Count; x++)
                    {
                        if (!present.Contains(x))
                        {
                            temp = x;
                        }
                    }

                    board[i, missing[0]] = temp;
                }
            }
            //missing.ForEach(Console.WriteLine);
        }
        DisplayBoard(board);
    }




    static void DisplayBoard(int[,])
    {
        for (int i = 0; i < 1; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                Console.Write(board[i, j]);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您没有为DisplayBoard方法

的参数命名
static void DisplayBoard(int[,] board)
{
    for (int i = 0; i < 1; i++)
    {
        for (int j = 0; j < 9; j++)
        {
            Console.Write(board[i, j]);
        }
    }
}