困惑于代码示例的Java输出

时间:2016-07-20 21:17:39

标签: java if-statement output

因此,我已经在各种项目(通过游戏进行自我教学 - 最好的学习方式)中通过Java进行了一段时间的混乱,而且我现在已经获得了这个小代码片段。用来覆盖一项技能'用一个新的。它在大多数情况下都可以正常工作,但是在我得到不同版本的特定情况下。

这是我调查方法的代码:

public void copy(Skill oldSkill) {
    if (oldSkill != null){
        this.name = oldSkill.getName();
        this.level = 0;
        this.rank = SkillRank.DABBLER ;
        this.difficulty = oldSkill.getDifficulty();
        if (oldSkill.neededSkill1 != null) {
            this.neededSkill1 = oldSkill.neededSkill1;
            if (oldSkill.neededSkill2 != null) {
                this.neededSkill2 = oldSkill.neededSkill2;
                if (oldSkill.neededSkill3 != null) {
                    this.neededSkill3 = oldSkill.neededSkill3;
                }
            }
        }
    }
    else {
        this.name = "default";
        this.level = 0;
        this.rank = SkillRank.DABBLER ;
        this.difficulty = Difficulty.BASIC;
    }
}

这是我做过的最小的代码(我现在使用Delta来避免空指针):

System.out.println("\nCopy Test:");
Skill Delta = new Skill("Delta", Difficulty.BASIC);
Skill GammaCopy = Delta;
GammaCopy.copy(Gamma);
System.out.println("New Skill name (Gamma): "+GammaCopy.name );
System.out.println("New skill Difficulty (Basic): "+GammaCopy.difficulty.name() );
System.out.println("New skill level (0): "+GammaCopy.level);
System.out.println("New Skill Gama preReqs (Alpha, Beta, Theta): "+Gamma.neededSkill1.name +", "+Gamma.neededSkill2.name+", "+Gamma.neededSkill3.name );
System.out.println("New Skill GamaCopy preReqs (Alpha, Beta, Theta): "+GammaCopy.neededSkill1.name +", "+GammaCopy.neededSkill2.name+", "+GammaCopy.neededSkill3.name );  

这是我在minitest上所期望的输出(并且除了在一个特定情况下得到):

Copy Test:
New Skill name (Gamma): Gamma
New skill Difficulty (Basic): BASIC
New skill level (0): 0
New Skill Gama preReqs (Alpha, Beta, Theta): Alpha, Beta, Theta
New Skill GamaCopy preReqs (Alpha, Beta, Theta): Alpha, Beta, Theta

如果我更换了Delta'以前初始化的技能称为“Alpha' (或Beta或Theta)。新的技能打印输出如下:

New Skill Gama preReqs (Alpha, Beta, Theta): Gamma, Beta, Theta
New Skill GamaCopy preReqs (Alpha, Beta, Theta): Gamma, Beta, Theta

我不知道为什么。我试过关闭文件和输出窗口并重做它。这并没有解决它(没有真正期望它)所以它不是像你有时可以用Matlab获得的存储变量......

任何人都可以解释为什么会这样吗?

0 个答案:

没有答案