控制台提示用户输入他的数组,我可以在用户输入输入之前输入0的col,我需要的是在用户完成后在数组的末尾添加0的col
谢谢
答案 0 :(得分:1)
for (int row = 0; row < r; row++)
{
for (int col = 0; col < c-1; col++)
{
Console.Write("Enter value for matrix[{0},{1}] = ", row, col - 1);
matrix[row, col] = (int)Convert.ToInt32(Console.ReadLine());
}
matrix[row, col] = 0;
}
答案 1 :(得分:1)
我确信您使用此代码在矩阵之前插入0。 How to add a column to the an array C#
此代码所需的更改是
//c++; //remove this line
c = c + 2; //add two extra column for adding 0's, One for beginning one for end
int[,] matrix = new int[r, c];
其他更改
for (int row = 0; row < r; row++)
{
for (int col = 0; col < c - 1; col++) // reduce column loop by one
{
if (col == 0)
{
matrix[row, col] = 0;
}
else
{
Console.Write("Enter value for matrix[{0},{1}] = ", row, col - 1);
matrix[row, col] = (int)Convert.ToInt32(Console.ReadLine());
}
}
}