如何获取3x3矩阵的输入 [英] How to I get the input for 3x3 matrix
本文介绍了如何获取3x3矩阵的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我被要求获得矩阵输入。
我是通过逐行获取输入来实现的。
如
1
2
3
4
5
6
7
8
9
但我被要求以矩阵格式输入
1(空格)2(空格)3
4 (空格)5(空格)6
7(空格)8(空格)9
有没有解决办法。
I was asked to get the matrix input.
I did it by getting the input line by line.
Such as
1
2
3
4
5
6
7
8
9
But I was asked to get the input as in matrix format
1(Space)2(Space)3
4(Space)5(Space)6
7(Space)8(Space)9
Is there any solution to do so.
推荐答案
试试这个
Try this
Console.WriteLine("input:3*3 matrics. number space number space number space enter");
int[,] input = new int[3, 3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
var key = Console.ReadKey();
if (key.Key != ConsoleKey.Spacebar && key.Key != ConsoleKey.Enter)
input[i, j] = Convert.ToInt32(key.KeyChar.ToString());
else
j--;
if (key.Key == ConsoleKey.Enter)
Console.WriteLine();
}
}
Console.WriteLine();
Console.WriteLine("output");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(input[i,j] + " ");
}
Console.WriteLine( );
}
这篇关于如何获取3x3矩阵的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文