问题描述
所以我做了一个2d数组,我想在其中添加数据,但我总是得到这个错误
so i made a 2d array and i want to add data into it but i always get this error
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in kwaterminal.exe
这是我怎么做的:
this how i did it:
int[,] terain = new int[x1, y1];
int placedmas = 0;
while (placedmas < LandMasa)
{
Random rnd = new Random();
int x = 0;
int y = 0;
x = rnd.Next(0,x1);
y = rnd.Next(0,y1);
terain[x1,y1] = 1; //here i get the error
}
我尝试了什么:
在线搜索错误但是我找不到那个试图像我一样做的人
What I have tried:
searchin the error online but i could not find someone who was trying to do the same as me
推荐答案
terain[x1,y1] = 1;
你可能意味着 [x,y]
(你随机变量)生成)。 x1
和 y1
是您之前指定的尺寸,因此此数组的右下角元素将位于 [x1 - 1,y1 - 1]
使 [x1,y1]
超出范围。
您还需要增加 placemas
在某些时候,或者你会陷入无限循环。
You probably meant [x, y]
(the variables you just randomly generated). x1
and y1
are the dimensions you specified earlier, so the "bottom-right" element of this array will be at [x1 - 1, y1 - 1]
which makes [x1, y1]
out of range.
You'll also have to increase placedmas
at some point, or you'll get stuck in an infinite loop.
这篇关于C#发生了'system.indexoutofrangeexception'类型的未处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!