如何将输入提供给矩阵 [英] How to feed input to a matrix
本文介绍了如何将输入提供给矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要以矩阵格式将输入提供给矩阵。
for 3X3矩阵我想提供如下输入
I need to feed input to a matrix in matrix format.
for 3X3 matrix I want to give input as follows
1 2 3
4 5 6
7 8 9
我想在上面的路线中给出输入。
有没有想要这样做。
这是我的代码
I want to give input in the above alignment.
Is there any want to do so.
Here is my code
Console.Write("Enter Number of Rows: ");
int nrows = int.Parse(Console.ReadLine());
Console.Write("\nEnter Number of Columns: ");
int ncolumns = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the elements of the Matrix Seperated by TAB Key Control+C to Exit\n");
int[,] input = new int[3, 3];
string[] input1 = new string[3];
for (int row = 0; row < nrows; row++)
{
string inp = Console.ReadLine();
string[] values = inp.Split('\t', ',',' ');
if (values.Length != ncolumns)
{
Console.WriteLine("\n\nEnter only {0} values for columns",ncolumns);
row--;
continue;
}
for (int column = 0; column < ncolumns; column++)
{
int val;
if (!int.TryParse(values[column], out val))
{
Console.WriteLine("\n\nEnter only Numbers\n");
row--;
break;
}
else
{
input[nrows, ncolumns] = val;
}
}
}
for (int i = 0; i < ncolumns; i++)
{
for (int j = 0; j < nrows; j++)
{
Console.Write(input[j, i] + "\t");
}
Console.WriteLine("\n\n");
}
Console.ReadLine();
推荐答案
看到这个,
http://stackoverflow.com/questions/19220942/creating-a-3x3-matrix-with-user -input-numbers-c-sharp [ ^ ]
-KR
See this,
http://stackoverflow.com/questions/19220942/creating-a-3x3-matrix-with-user-input-numbers-c-sharp[^]
-KR
这篇关于如何将输入提供给矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文