嗨,我对编码很陌生,正试图弄清楚如何创建一个2d数组。这要求用户输入行和列的大小,而不是用随机数填充所选的行和列大小。经过几天的努力,我决定来这里寻求帮助。这是我到目前为止所拥有的,它遍布我所知道的所有地方。
char row = 'i';
char column = 'j';
int[,] twoDarray = new int[row, column];
int min = 0;
int max = 100;
Random randNum = new Random();
for (int i = 0; i < twoDarray.Length; ++i)
for (int j = 0; j < twoDarray.Length; ++j)
{
twoDarray[i, j] = Convert.ToInt32(In.ReadLine());
}
不确定我是否理解你的问题。我假设它是一个控制台应用程序。在这种情况下,代码可能看起来像:
int row, column;
string rowVal;
Console.Write("nEnter Number of Rows: ");
rowVal = Console.ReadLine();
string colVal;
Console.Write("nEnter Number of Columns: ");
colVal = Console.ReadLine();
bool allGood = true;
if(!int.TryParse(rowVal, out row))
{
allGood = false;
Console.Write("Number of Rows is wrong: ");
}
if (!int.TryParse(colVal, out column))
{
allGood = false;
Console.Write("Number of Columns is wrong: ");
}
if (!allGood)
throw new Exception("Wrong input data"); //or just return from your method
int[,] twoDarray = new int[row, column];
int min = 0;
int max = 100;
Random randNum = new Random();
for (int i = 0; i < twoDarray.GetLength(0); ++i)
{
for (int j = 0; j < twoDarray.GetLength(1); ++j)
{
twoDarray[i, j] = randNum.Next(min,max);
}
}