接受C#中的'n x n'矩阵

时间:2015-12-24 12:34:47

标签: c# matrix

我已经被这个想法搞砸了一段时间。 在c#中接受带空格的数组并不简单。我们如何接受二维数组,其大小由用户给出?我试过这个,但有些事情是不对的。非常感谢帮助。谢谢。

      Input:
            4
            1 2 3 4
            2 3 4 5
            3 4 5 6
            4 5 6 7

第一行指定'n x n'

中'n'的值

嗯,我试过的代码就是这个。对于你们中的一些人来说,这可能看起来很愚蠢:

        string input;
        string[] inputArray;
        int[] inputInt;

        int MxM = Convert.ToInt32(Console.ReadLine()); //MxM = First Line=n
        int[,] array = new int[MxM, MxM];
        for (int i = 0; i < MxM; i++)
        {
            for (int j = 0; j < MxM; j++)
            {
                input = Console.ReadLine();
                inputArray = input.Split(' ');
                inputInt = new int[MxM];
                for (int k = 0; k < MxM; k++)
                {
                    inputInt[k] = int.Parse(inputArray[k]);
                    array[i, j] = inputInt[k];
                }
            }
        }

希望,答案也是输出矩阵的答案。谢谢

4 个答案:

答案 0 :(得分:1)

或者:

for (int i = 0; i < MxM; i++)
  for (int j = 0; j < MxM; j++)
    array[i, j] = Convert.ToInt32(Console.ReadLine());

或:

for (int i = 0; i < MxM; i++)
{
  inputArray = Console.ReadLine().Split(' ');
  for (int j = 0; j < MxM; j++)
    array[i, j] = Convert.ToInt32(inputArray[j]);
}

当然,如果你没有以正确的格式输入,那么这可能会失败。

答案 1 :(得分:0)

您可以尝试使用以下代码:

  

Console.ReadLine()。Split()。选择(str =&gt; Convert.ToInt32(str))。ToArray()

从行中获取一个int数组。

我认为你理解这个想法。

答案 2 :(得分:0)

public static int[,] MatrixConstructor()
{
    int N = Convert.ToInt32(Console.ReadLine());
    int[,] matrix = new int[N, N];
    for (int i = 0; i < N; i++)
    {
        String[] line = Console.ReadLine().Split(' ');
        for (int j = 0; j < N; j++)
        {
            matrix[i,j] = Convert.ToInt32(line[j]);
        }
    }

    return matrix;
}

这符合您的意见。

答案 3 :(得分:0)

如果我理解你想要的东西:

您希望在示例N中输入4,然后创建一个4 x 4数组。然后,对于第一维中的每个索引,请求用户输入等于N由空格分隔的整数数。读取该输入并将其放入适当的2d索引的数组中。因此,以4作为第一个输入,将提示用户:

Please input 4 integers(separated by spaces):

用户可以键入,例如:

Please input 4 integers(separated by spaces): 1 2 3 4

然后点击进入。再次提示用户:

Please input 4 integers(separated by spaces): 1 2 3 4
Please input 4 integers(separated by spaces):

依此类推:

Please input 4 integers(separated by spaces): 1 2 3 4
Please input 4 integers(separated by spaces): 2 3 4 5
Please input 4 integers(separated by spaces): 3 4 5 6
Please input 4 integers(separated by spaces): 4 5 6 7

在这种情况下,您可能会使用以下内容:

class Program
{
    static void Main(string[] args)
    {
        int size;
        Console.WriteLine("Please enter the size of the matrix:");
        if (!int.TryParse(Console.ReadLine(), out size))
        {
            Console.WriteLine("The value provided for the size was not a proper integer value.");
            Console.WriteLine("Press ESC to quit...");
            while (Console.ReadKey().Key != ConsoleKey.Escape) { }
            return;
        }

        int[,] matrix = new int[size, size];

        for (int i = 0; i < size; ++i)
        {
            bool complete = false;
            while (!complete)
            {
                Console.WriteLine(string.Format("[{0}] - Please input {1} integers(separated by spaces):", i, size));
                string[] input = Console.ReadLine().Split(' ');
                if (input.Count() != size)
                {
                    Console.WriteLine("The input was invalid, try again...");
                    continue;
                }
                for (int j = 0; j < size; ++j)
                {
                    if (!int.TryParse(input[j], out matrix[i, j]))
                    {
                        complete = false;
                        Console.WriteLine("The input was invalid, try again...");
                        break;
                    }
                    complete = true;
                }

            }
        }
        Console.WriteLine("Output: \n");
        WriteMatrix(matrix, size);
        Console.ReadKey();
    }

    private static void WriteMatrix(int[,] matrix, int size)
    {
        string output = string.Empty;
        for(int i = 0; i < size; ++i)
        {
            string line = string.Empty;
            for (int j = 0; j < size; ++j)
                line += string.Format("{0} ", matrix[i, j]);
            output += string.Format("{0}\n", line.Trim());
        }
        Console.WriteLine(output);
    }

}

上面的代码对于用户输入无效值几乎是安全的,但你可能花一些时间来清理它或者进一步增强它。基本的想法就在那里。