我试图在Java中组合一个小游戏概念,但遇到了一个奇怪的问题。 int数组似乎在代码的某些部分中没有存储任何值!
(使用netbeans 8.2)
我怀疑这是我错过的小而乏味的事情。
这是基本Creature的构造函数。看到“ addTrait”方法了吗?这是重点。

public Creature(String inputname)
{
    if(DEBUG>0){System.out.println("DEBUG: CREATURE ");}

    creaturename = inputname;
    size = 1;
    population = 1;

    addTrait(new Trait("TEST",0,new int[]{4,4,4,4,4,4}),0);
    addTrait(new Trait("EMPTY",0,new int[]{0,0,0,0,0,0}),1);
    addTrait(new Trait("EMPTY",0,new int[]{0,0,0,0,0,0}),2);

}


这是Creature类中的方法。

public void addTrait(Trait newtrait, int slotnum)
{
    myTraits[slotnum] = newtrait;

    if(DEBUG>0){System.out.println("DEBUG: CREATURE > "
            + "addTrait > myTraits["+slotnum+"].getName() = "+
            myTraits[slotnum].getName());}


        for(int k=0;6>k;k++)
        {
            System.out.println("DEBUG: CREATURE > "
            + "addTrait > myTraits["+slotnum+"].getEffects()["+k+"] = "+
            myTraits[slotnum].getEffects()[k]);
        }

        System.out.println();
}


在驱动程序类中,我尝试将“ Sharp Eyesight”特征添加到我的测试生物中。

public class EvoDriver {
public static void main(String[] args) {

    // initilize TraitList
    TraitList myTraits = new TraitList();

    // initilize Creature

    Creature player1 = new Creature("Dragon Bear");

    player1.addPop();
    player1.addPop();
    player1.growSize();

    // add specific trait

    //player1.addTrait(myTraits.getTrait("Carnivore"), 0);
    player1.addTrait(myTraits.getTrait("Sharp Eyesight"), 1);
    //player1.addTrait(myTraits.getTrait("Fire Breath"), 2);

    //force update
    player1.updateStats();
    //print creature
    player1.printCreature();



}


这是特征的构造函数。

public Trait(String newname, int fv, int[] effects)
{
    if(DEBUG>0){System.out.println("DEBUG: TRAIT");}

    traitname = newname;
    feedvalue = fv;

    //handle effects
    effects[0] = feedertype;
    effects[1] = size;
    effects[2] = power;
    effects[3] = stealth;
    effects[4] = senses;
    effects[5] = dangerous;

    if(DEBUG>0){System.out.println("DEBUG: TRAIT > traitname = "+traitname);}
    if(DEBUG>0){System.out.println("DEBUG: TRAIT > senses = "+senses);}

}


我有一个类,应该为每个称为TraitList的特征处理启动统计提升的过程。它保存每个特征的所有数据。但是,当调用方法“ getEffects”时,结果始终为0!

public class TraitList {

//DEBUG MODE
final int DEBUG = 1;



Trait[] allTraits = {new Trait("EMPTY",0,new int[]{0,0,0,0,0,0})};

// **** Constructors ****

/*                              HINTS
____________________________________________________________________________
effects[0] = feedertype;
effects[1] = size;
effects[2] = power;
effects[3] = stealth;
effects[4] = senses;
effects[5] = dangerous;
*/


public TraitList()
{

    if(DEBUG>0){System.out.println("DEBUG: TRAITLIST ");}



    boolean[] special = new boolean[2];
    special[0] = false; //Alert the Herd
    special[1] = false; //Pack Tactics

    //add effects to list

    // Sharp Eyesight; increases Senses by 3

    //feedertype;
    //size;
    //power;
    //stealth;
    //senses;
    //dangerous;
    addTrait(new Trait("Sharp Eyesight", 2, new int[]{0,0,0,0,3,0} )
            .setDesc("Helps detect other creatures!"));

    // Camouflage; increases Stealth by 3

    addTrait(new Trait("Camouflage", 0, new int[]{0,0,0,0,0,0}));

    // Alert the Herd; increases Senses by Population number
    addTrait(new Trait("Alert the Herd", 0, special));

    // Pack Tactics; increases Power by Population number
    addTrait(new Trait("Pack Tactics", 0, special));

    // Carnivore; creature can only eat meat; Senses +1, Stealth +1

    addTrait(new Trait("Carnivore", -1, new int[]{0,0,0,0,0,0}));

    // Omnivore; creature can eat meat and plants

    addTrait(new Trait("Omnivore", -5, new int[]{0,0,0,0,0,0}));

    // Horns; increases Dangerous by 1

    addTrait(new Trait("Horns", 0, new int[]{0,0,0,0,0,0}));

    // Fire Breath; increases Dangerous by 3, increases Power by 3; feed 10

    addTrait(new Trait("Fire Breath", 10, new int[]{0,0,0,0,0,0}));
}

// **** Methods ****

public void addTrait(Trait newtrait)
{
    // create temp array
    Trait[] temp = new Trait[1];
    temp[0] = newtrait;
    Trait[] newtemp = new Trait[allTraits.length+1];
    for (int k=0; allTraits.length>k;k++)
    {
        newtemp[k] = allTraits[k];
    }
    newtemp[allTraits.length] = temp[0];

    allTraits = newtemp;

}

public Trait getRandomTrait() // to do
{
    return null;
}

public int getTraitIndex(String searchname)//returns 0 if not found
{

    for(int k=0;allTraits.length>k;k++)
    {
        if(DEBUG>0)
        {System.out.println("DEBUG: TRAITLIST > getTraitIndex > k = "+k);}

        if(DEBUG>0)
        {System.out.println("DEBUG: TRAITLIST > getTraitIndex > "
                + "allTraits[k].getName() = "+allTraits[k].getName());}

        if(searchname.equalsIgnoreCase(allTraits[k].getName()))
        {
            return k;
        }
    }
    System.out.println("ERROR IN TraitList > getTraitIndex! TRAIT "
            + searchname +" DOES NOT EXIST!");
    return 0;
}

public Trait getTrait(String name)//returns EMPTY trait if not found
{
    return allTraits[getTraitIndex(name)];
}


}

我不知道自己之前有多清楚,但是如果有任何提示,建议,我将不胜感激,我会做出有益的侮辱指出我的愚蠢错误。我将附加调试输出。

run:
DEBUG: TRAIT
DEBUG: TRAIT > traitname = EMPTY
DEBUG: TRAIT > senses = 0
DEBUG: TRAITLIST
DEBUG: TRAIT
DEBUG: TRAIT > traitname = Sharp Eyesight
DEBUG: TRAIT > senses = 0
DEBUG: TRAIT
DEBUG: TRAIT > traitname = Camouflage
DEBUG: TRAIT > senses = 0
DEBUG: TRAIT
DEBUG: TRAIT > traitname = Carnivore
DEBUG: TRAIT > senses = 0
DEBUG: TRAIT
DEBUG: TRAIT > traitname = Omnivore
DEBUG: TRAIT > senses = 0
DEBUG: TRAIT
DEBUG: TRAIT > traitname = Horns
DEBUG: TRAIT > senses = 0
DEBUG: TRAIT
DEBUG: TRAIT > traitname = Fire Breath
DEBUG: TRAIT > senses = 0
DEBUG: CREATURE
DEBUG: TRAIT
DEBUG: TRAIT > traitname = TEST
DEBUG: TRAIT > senses = 0
DEBUG: CREATURE > addTrait > myTraits[0].getName() = TEST
DEBUG: CREATURE > addTrait > myTraits[0].getEffects()[0] = 0
DEBUG: CREATURE > addTrait > myTraits[0].getEffects()[1] = 0
DEBUG: CREATURE > addTrait > myTraits[0].getEffects()[2] = 0
DEBUG: CREATURE > addTrait > myTraits[0].getEffects()[3] = 0
DEBUG: CREATURE > addTrait > myTraits[0].getEffects()[4] = 0
DEBUG: CREATURE > addTrait > myTraits[0].getEffects()[5] = 0

DEBUG: TRAIT
DEBUG: TRAIT > traitname = EMPTY
DEBUG: TRAIT > senses = 0
DEBUG: CREATURE > addTrait > myTraits[1].getName() = EMPTY
DEBUG: CREATURE > addTrait > myTraits[1].getEffects()[0] = 0
DEBUG: CREATURE > addTrait > myTraits[1].getEffects()[1] = 0
DEBUG: CREATURE > addTrait > myTraits[1].getEffects()[2] = 0
DEBUG: CREATURE > addTrait > myTraits[1].getEffects()[3] = 0
DEBUG: CREATURE > addTrait > myTraits[1].getEffects()[4] = 0
DEBUG: CREATURE > addTrait > myTraits[1].getEffects()[5] = 0

DEBUG: TRAIT
DEBUG: TRAIT > traitname = EMPTY
DEBUG: TRAIT > senses = 0
DEBUG: CREATURE > addTrait > myTraits[2].getName() = EMPTY
DEBUG: CREATURE > addTrait > myTraits[2].getEffects()[0] = 0
DEBUG: CREATURE > addTrait > myTraits[2].getEffects()[1] = 0
DEBUG: CREATURE > addTrait > myTraits[2].getEffects()[2] = 0
DEBUG: CREATURE > addTrait > myTraits[2].getEffects()[3] = 0
DEBUG: CREATURE > addTrait > myTraits[2].getEffects()[4] = 0
DEBUG: CREATURE > addTrait > myTraits[2].getEffects()[5] = 0

DEBUG: TRAITLIST > getTraitIndex > k = 0
DEBUG: TRAITLIST > getTraitIndex > allTraits[k].getName() = EMPTY
DEBUG: TRAITLIST > getTraitIndex > k = 1
DEBUG: TRAITLIST > getTraitIndex > allTraits[k].getName() = Sharp Eyesight
DEBUG: CREATURE > addTrait > myTraits[1].getName() = Sharp Eyesight
DEBUG: CREATURE > addTrait > myTraits[1].getEffects()[0] = 0
DEBUG: CREATURE > addTrait > myTraits[1].getEffects()[1] = 0
DEBUG: CREATURE > addTrait > myTraits[1].getEffects()[2] = 0
DEBUG: CREATURE > addTrait > myTraits[1].getEffects()[3] = 0
DEBUG: CREATURE > addTrait > myTraits[1].getEffects()[4] = 0
DEBUG: CREATURE > addTrait > myTraits[1].getEffects()[5] = 0

_________________________________________________
Dragon Bear (Pop: 3)
[Herbivore]
_________________________________________________
Size: 2
Power: 2
Stealth: 0
Senses: 0
Dangerous: 0
_________________________________________________
Trait 1: TEST
 ~ {  }
Trait 2: Sharp Eyesight
 ~ { Helps detect other creatures! }
Trait 3: EMPTY
 ~ {  }
_________________________________________________
BUILD SUCCESSFUL (total time: 0 seconds)


如果找不到解决方案,我可能会从头开始。 (我当然会重用一些工作代码!)

最佳答案

在构造器中,您正在重新分配效果的值。这是潜在的问题。

effects[0] = feedertype;
effects[1] = size;
effects[2] = power;
effects[3] = stealth;
effects[4] = senses;
effects[5] = dangerous;

关于java - 尝试调试int数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46209907/

10-11 02:43