[英]Accepting a 'n x n' Matrix in C#
我已经被这个想法困扰了一段时间了。 在C#中,接受带有空格的数组不是直截了当的。 我们如何接受用户指定大小的二维数组? 我试过了,但是不对。 非常感谢您的帮助。 谢谢。
Input:
4
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
第一行在“ nx 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];
}
}
}
希望答案也将是输出矩阵的答案。 谢谢
要么:
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]);
}
当然,如果您输入的格式不正确,则可能会失败。
您可以尝试使用如下代码:
Console.ReadLine()。Split()。Select(str => Convert.ToInt32(str))。ToArray()
得到一个int数组。
我想你明白这个主意。
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;
}
这符合您的输入。
如果我正确理解您的要求:
您想在示例4
输入N
,创建一个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);
}
}
上面的代码在大多数情况下应该对用户输入无效值是安全的,但是您可能需要花费一些时间来清理或增强它。 基本思想在那里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.