C#发生了'system.indexoutofrangeexception'类型的未处理异常 [英] C# an unhandled exception of type 'system.indexoutofrangeexception' occurred in

查看:365
本文介绍了C#发生了'system.indexoutofrangeexception'类型的未处理异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我做了一个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'类型的未处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆