C# 如何通过用户输入填充二维阵列?

C# 如何通过用户输入填充二维阵列?,c#,asp.net,.net,C#,Asp.net,.net,我试图在C#中创建一个5,4数组,其中每个单元格由用户数据组成。下面的代码有时有效,但并不一致。例如,我可以将数据输入单元格[4,5]或[3,4]。但是,有时当我运行代码时,它不接受数据并显示空单元格。有什么建议吗 decimal[,] departments = new decimal[4, 5]; //int count = 0; Console.WriteLine("If you would like to run the program,

我试图在C#中创建一个5,4数组,其中每个单元格由用户数据组成。下面的代码有时有效,但并不一致。例如,我可以将数据输入单元格[4,5]或[3,4]。但是,有时当我运行代码时,它不接受数据并显示空单元格。有什么建议吗

        decimal[,] departments = new decimal[4, 5];

        //int count = 0;
        Console.WriteLine("If you would like to run the program, type enter");
        string exit = Console.ReadLine();       
        while (exit != "yes")
        {
            Console.WriteLine("What is the department number that you would like to enter sales for? Enter 9 to exit");
            int departmentNo = int.Parse(Console.ReadLine());
            Console.WriteLine("What day would you like to enter sales for?");
            int input = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter sales");
            departments[departmentNo, input] = decimal.Parse(Console.ReadLine());
            Console.WriteLine("If you are finished, enter yes");
            exit = Console.ReadLine();

        }
示例输出

0 0 0 0
0 0 0 0
0 0 500 0
500 0 0 0

您可以使用
.GetUpperBound(dimension)
获取特定维度的最大界限(在下面的示例中,每个维度将返回1)。一旦你做到了这一点,你只需要浏览每个职位(我假设你只需要填写每个职位的数据)

以下是一个示例:

int[,] items = new int[2, 2];
int width = items.GetUpperBound(0) + 1;
int height = items.GetUpperBound(1) + 1;
for (int x = 0; x < width; ++x)
{
    for (int y = 0; y < height; ++y)
    {
        string input;
        int inputValue;
        do
        {
            Console.WriteLine($"Please input value for ({x},{y}): ");
        }
        while (!int.TryParse(input = Console.ReadLine(), out inputValue));
        items[x, y] = inputValue;
    }
}
int[,]项=新的int[2,2];
int width=items.GetUpperBound(0)+1;
int height=items.GetUpperBound(1)+1;
对于(int x=0;x
或者,如果您确实需要用户能够指定销售值的去向,则可以更新代码以添加一些验证检查:

while (exit != "yes")
{
    Console.WriteLine("What is the department number that you would like to enter sales for? Enter 9 to exit");
    int departmentNo = int.Parse(Console.ReadLine());
    if (departmentNo > departments.GetUpperBound(0) || departmentNo < 0)
    {
        Console.WriteLine("Please enter a valid department number.");
        continue;
    }
    Console.WriteLine("What day would you like to enter sales for?");
    int input = int.Parse(Console.ReadLine());
    if (input > departments.GetUpperBound(1) || input < 0)
    {
        Console.WriteLine("Please enter a valid input.");
        continue;
    }

    Console.WriteLine("Enter sales");
    if (!decimal.TryParse(Console.ReadLine(), out decimal value))
    {
        Console.WriteLine("Please enter a valid sales figure.");
        continue;
    }
    departments[departmentNo, input] = value;
    Console.WriteLine("If you are finished, enter yes");
    exit = Console.ReadLine();
}
while(退出!=“是”)
{
Console.WriteLine(“您想要输入销售的部门号是什么?输入9退出”);
int departmentNo=int.Parse(Console.ReadLine());
if(departmentNo>departments.GetUpperBound(0)| | departmentNo<0)
{
Console.WriteLine(“请输入有效的部门号”);
继续;
}
Console.WriteLine(“您希望输入销售日期?”);
int input=int.Parse(Console.ReadLine());
如果(输入>部门.GetUpperBound(1)| |输入<0)
{
Console.WriteLine(“请输入有效的输入。”);
继续;
}
Console.WriteLine(“输入销售”);
if(!decimal.TryParse(Console.ReadLine(),out decimal value))
{
Console.WriteLine(“请输入有效的销售数字”);
继续;
}
部门【部门号,输入】=值;
Console.WriteLine(“如果完成,请输入yes”);
exit=Console.ReadLine();
}

您可能希望将其更改为类似于上面的示例,以便在输入错误时不必重新启动整个循环。

因为
部门的
具有固定长度
[4,5]
,这意味着
部门编号
只能在范围
0-3
内,而
输入
只能在范围
0-4
内。此外,
decimal
关键字表示128位数据类型,它表示单元格的值在
±1.0 x 10^28到±7.9228 x 10^28
范围内,表示大约28-29位有效数字。您应该设置一些
if
条件,以确保输入值在可用范围内。

数组的索引为零,以将其考虑在内。另外,我个人会创建一个类来使用泛型存储数据,以使其更容易。你说的“它不接受数据”是什么意思?