简体   繁体   English

非常大的特定2d数组for循环在随机点中返回空值

[英]Very large, specific 2d array for-loop returning null values in random spots

I have a huge double for loop to fill a 2d array, based on a very specific set of criteria. 根据一组非常具体的条件,我有一个巨大的double循环来填充2d数组。 I'm displaying this 2d array in a JOptionPane; 我在JOptionPane中显示此2d数组; on some runs of the program, I get no "null", on others, I get lots of them. 在程序的某些运行上,我没有“ null”,在其他程序上,我得到了很多。 There doesn't appear to be a pattern of where/when it occurs. 似乎没有发生它的位置/时间的模式。

This leads me to believe my issue has to deal with my "else if" criteria. 这使我相信我的问题必须处理我的“否则”标准。

My array is filled with different strings depending on: If there are Monsters in the cell, if there is a piece of Armour, if there is a Weapon, if there is Nothing, or if there is a combination of any assortment of the above (monster + weapon, monster + armour, etc). 我的数组充满了不同的字符串,具体取决于:如果牢房中有怪物,是否有一件盔甲,是否有武器,如果什么都没有,或者以上各项的组合(怪物+武器,怪物+盔甲等)。

This has lead me to make the following chain of if/else if: 这导致我进行以下if / else if链:

if(map[a][b] == map[0][0])      //sets the first cell as the entrance
...
else if(!x.hasAM()  && armour == 0 && weapons == 0)         //has nothing
...
else if(!x.hasAM() && armour > 0 && weapons == 0)           //has armour
...
else if(!x.hasAM() && armour == 0 && weapons > 0)           //has weapon
...
else if(x.hasAM() && armour == 0 && weapons == 0)           //has monster
...
else if(x.hasAM() && armour > 0 && weapons == 0)        //has monster, has armour
...
else if(x.hasAM() && armour == 0 && weapons > 0)        //has monster, has weapon
...
else if(x.hasAM() && armour > 0 && weapons > 0)     //has monster, has armour, has weapon

In some of these, the way I assign the string is simply "map[a][b] = "|________|";". 在其中的某些方法中,我分配字符串的方式只是“ map [a] [b] =“ | ________ |”;“。 In others I have nested Switches. 在其他情况下,我嵌套了开关。 I don't think that's the issue, though, because the strings that are generated by the nested Switches work. 不过,我认为这不是问题所在,因为嵌套的Switches生成的字符串可以工作。 But obviously I have no clue what the issue is, so just in case here is an example of the most "complex" set up: 但是显然我不知道问题出在哪里,因此以防万一,这是最“复杂”设置的示例:

else if (Player.type.equalsIgnoreCase("Ranger")) {
    if (Cell.monsters.length == 4) {
        switch (weapons) {
            case 1:
                switch (armour) {
                    case 1:
                        map[a][b] = "|_!Sl@L__XXXX|";
                        break;

                    case 2:
                        map[a][b] = "|_!Sl@C__XXXX|";
                        break;

                    case 3:
                        map[a][b] = "|_!Sl@P__XXXX|";
                        break;

                    case 4:
                        map[a][b] = "|_!Sl@M__XXXX|";
                        break;
                }
                break;
        }
    }
}

The issue I'm having is that sometimes the output of my program is like this: 我遇到的问题是有时程序的输出如下所示:

|_!Sl@M__XXXX| |_!Sl@M__XXXX| null null |_!Sl@M__XXXX|
null |_!Sl@M__XXXX| |_!Sl@M__XXXX| null null

etc. 等等

Here is the full method, in case I'm wrong about it being my "else if" statements and you want to look at the whole thing: https://gist.github.com/anonymous/7518098 这是完整的方法,以防万一我把它当作我的“ else if”语句,而您想看整个事情的话: https : //gist.github.com/anonymous/7518098

And as always, thanks in advance! 和往常一样,提前致谢! :D :D

Okay. 好的。 You're clearly missing some combination of armor, player type, weapons, and number-of-monsters in your long if - else if chain. 很显然,您在if - else if连锁店中缺少盔甲,玩家类型,武器和怪物数量的组合。 We could try to analyze it in detail, but the structure of your code makes that really difficult to do. 我们可以尝试对其进行详细分析,但是您的代码结构确实很难做到。 (the comments about restructuring your code are on point, even if you don't really know how to do that yet) (关于重组代码的注释是正确的,即使您实际上还不知道该怎么做)

Therefore, let's ask the program to tell us what combination is being missed. 因此,让我们让程序告诉我们缺少什么组合。

I would recommend that you add this in line 1275, right before the "builder.append" line: 我建议您在“ builder.append”行之前的第1275行中添加此代码:

if (map[a][b] == null) {
    System.out.println("Missed case! Player.type is " + Player.type
                       + " and weapons is " + weapons
                       + " and armour is " + armour
                       + " and Cell.monsters.length is " + Cell.monsters.length);
}

Then, run your program and find out which combination is being missed and trace it down. 然后,运行您的程序,找出缺少的组合并进行跟踪。 Then keep coding and working on your code structure because it can get better, but generally requires practice. 然后继续编码并继续使用您的代码结构,因为它可以变得更好,但通常需要进行实践。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM