试图调试int数组

时间:2017-09-14 03:11:31

标签: java arrays debugging netbeans-8

我正在尝试在java中组合一个小游戏概念,并遇到了一个奇怪的问题; int数组似乎没有在我的代码的某些部分存储任何值!
(使用netbeans 8.2)
我怀疑我错过了一些小而乏味的东西 这是基本生物的构造函数。看到“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启动stat提升。它包含每个特征的所有数据。但是,当调用方法“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)

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

1 个答案:

答案 0 :(得分:0)

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

scene.addEventHandler(KeyEvent.KEY_PRESSED, e -> {
    if (e.getCode() == KeyCode.ENTER) {
        Node focusOwner = scene.getFocusOwner();
        if (focusOwner instanceof Button) {
            ((Button) focusOwner).fire();
        }
    }
});