使数组索引反向检查列表时超出范围

时间:2018-10-27 03:41:12

标签: c# indexoutofboundsexception

因此,在解决上一期问题后,我去添加了一些代码,并且我一直在尝试在此处和其他地方查找无法解释我问题的任何内容,因此有办法制作此代码

for (int j = 0; j < 4; j++) {
    worldX = 0;
    worldY = 0;
    while (worldY != WorldHeight) { 
         while (worldX != WorldWidth) {
             if (worldY > 0) {
                 if (worldX > 0) {
                    if (world[worldX-1, worldY-1] == 1 && genran.Next(genran.Next(genran.Next(-7,-1),genran.Next(0,2)),genran.Next(genran.Next(5,8),genran.Next(9,16))) > 6) { world[worldX, worldY] = 1;}
                    } if ((worldX + 1) < WorldWidth) {
                    if (world[worldX+1, worldY-1] == 1 && genran.Next(genran.Next(genran.Next(-7,-1),genran.Next(0,2)),genran.Next(genran.Next(5,8),genran.Next(9,16))) > 6) { world[worldX, worldY] = 1;}
                 }
             }
             worldX++;
         }
         worldX = 0;
         worldY++;
     }
 }

变成这样

for (int j = 0; j < 4; j++) {
    worldX = WorldWidth;
    worldY = WorldHeight;
    while (worldY >= 0) { 
         while (worldX >= 0) {
             if (worldY > 0) {
                 if (worldX > 0) {
                    if (world[worldX-1, worldY-1] == 1 && genran.Next(genran.Next(genran.Next(-7,-1),genran.Next(0,2)),genran.Next(genran.Next(5,8),genran.Next(9,16))) > 6) { world[worldX, worldY] = 1;}
                    } if ((worldX - 1) > 0) {
                    if (world[worldX+1, worldY-1] == 1 && genran.Next(genran.Next(genran.Next(-7,-1),genran.Next(0,2)),genran.Next(genran.Next(5,8),genran.Next(9,16))) > 6) { world[worldX, worldY] = 1;}
                 }
             }
             worldX--;
         }
         worldX = WorldWidth;
         worldY--;
     }
 }

所以我不想从左上角开始,而是要从右下角开始,然后从那里倒退

PS。我还是其中的一些新手。

0 个答案:

没有答案