User input into a two dimensional array

前端 未结 6 1763
生来不讨喜
生来不讨喜 2021-01-07 13:44

I\'m completely new to C# and well I would like simple code to create a matrix from user input

E.G.

int [,] matrix1 = new int [2,2]
// now using inpu         


        
相关标签:
6条回答
  • 2021-01-07 14:19
    int[,] A = new int[5, 4];
    
    //read
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            A[i, j] = int.Parse(Console.ReadLine());  
        }
    }
    
    //Write
    for (int i = 0; i < 5; i++)
    {
        Console.WriteLine();
    
        for (int j = 0; j < 4; j++)
        {
            Console.Write(A[i, j]);
        }
    }
    
    0 讨论(0)
  • 2021-01-07 14:25

    var numbers = new int[size, size];

                for (var i = 0; i < size; i++)
                {
                    var numList = new string[size];
                    numList = readLine.Split();
                    for (var j = 0; j < size; j++)
                    {
                        numbers[i, j] =convert.ToInt32(numList[j]);                        
                    }
    

    }

    0 讨论(0)
  • 2021-01-07 14:32
    static void Main(string[] args)
    {
       int[,] matrix1 = new int[2, 2];
    
       for (int i = 0; i < 2; i++)
       {
             for (int j = 0; j < 2; j++)
             {
                matrix1[i, j] = Int32.Parse(Console.ReadLine());  
             }
       }
    
       for (int i = 0; i < 2; i++)
       {
              for (int j = 0; j < 2; j++)
              {
                 Console.WriteLine("Element({0},{1})={2}", i, j, matrix1[i, j]);
              }
       }
    }
    
    0 讨论(0)
  • 2021-01-07 14:35

    I was trying to find a good answer to that question in C#, I searched a lot until a friend helped me to understand how to do it with user input, SO I will leave the answer here maybe it could help anyone

    int n = int.Parse(Console.ReadLine()); //the size of the array
    int m = n;
    int[,] arr = new int[n, m];
    string[]lines = new string[n]; // so we could read the input from the user
    for (int i = 0; i < n; i++) // here we need to read more than one line
    {
        lines[i] = Console.ReadLine();
    }
    
    
    for (int i = 0; i < n; i++)
    {
        string[]num = lines[i].Split(' ');
        for (int j = 0; j < m; j++)
        {
            int z = Convert.ToInt32(num[j]);
            arr[i, j] = z;
        }
    }
    
    for (int i = 0; i < n; i++)
    {
        Console.WriteLine();
        for (int j = 0; j < m; j++)
        {
            Console.Write(arr[i, j] + " ");
        }
    }
    
    0 讨论(0)
  • 2021-01-07 14:39
    Console.WriteLine("Enter the height: ");
    int h = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("Enter the width: ");
    string w = Convert.ToInt32(Console.ReadLine());
    
    int[,] arr = new int[w, h];
    
    for (int i = 0; i < w; ++i)
      for (int j = 0; j <h; ++j)
        arr[i, j] = Convert.ToInt32(Console.ReadLine());
    
    0 讨论(0)
  • 2021-01-07 14:42
    var numbers = new int[size, size];
    
    for (var i = 0; i < size; i++)
    {
        var numList = new string[size];
        numList = readLine.Split();
        for (var j = 0; j < size; j++)
        {
            numbers[i, j] = Convert.ToInt32(numList[j]);     
        }
    }
    
    0 讨论(0)
提交回复
热议问题