用户输入二维数组

时间:2013-03-29 06:44:44

标签: c#

我对C#完全陌生,我想要简单的代码来创建用户输入的矩阵

E.G。

int [,] matrix1 = new int [2,2]
// now using input i'd like to add integers into the array
matrix1[0,1] = Int32.Parse(Console.ReadLine()); // this is for user input

等等。

6 个答案:

答案 0 :(得分:1)

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]);
          }
   }
}

答案 1 :(得分:1)

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]);
    }
}

答案 2 :(得分:1)

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]);     
    }
}

答案 3 :(得分:0)

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());

答案 4 :(得分:0)

var numbers = new int [size,size];

<form action="/Home/TestForm" method="post">    <div style="display: table">

    <div style="display: table-row">
        <div style="display: table-cell"><label for="Cmd">CMD String</label></div>
        <div style="display: table-cell">
            <input class="CmdForm" id="Cmd" name="Cmd" type="text" value="" />
        </div>
    </div>

    <div style="display: table-row">
        <div style="display: table-cell"><label class="MacAddress" for="MacAddress">MAC Address</label></div>
        <div style="display: table-cell">
            <input id="MacAddress" name="MacAddress" type="text" value="" />
        </div>
    </div>

    <div style="display: table-row">
        <div style="display: table-cell"></div>
        <div style="display: table-cell">
            <input type="button" value="Submit"/>
        </div>
    </div>
</div>

}

答案 5 :(得分:0)

我试图在C#中为该问题找到一个很好的答案,我进行了很多次搜索,直到有朋友帮助我了解如何通过用户输入来解决这个问题,所以我将在这里留下答案,也许它可以帮助任何人

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] + " ");
    }
}