如果在if条件中使用了一个布尔方法,它会在返回true之前运行吗?

huangapple go评论67阅读模式
英文:

If a boolean method is used inside an if condition, does it run before returning true?

问题

我是Java中文翻译,代码部分不进行翻译,以下是翻译好的内容:

我刚接触Java,正在处理一个要求我设置“防御塔”游戏的任务,其中涉及昆虫。游戏中有方块(tiles),在你创建昆虫并将它们放在方块上之前,你必须检查昆虫是否“适合”。

我在为抽象类“Insect”的子类Hornet设置构造函数方面遇到了问题。目前,每当下面的代码运行时,会将两个新的Hornet放入“Swarm”数组中,而不是一个(Hornet对象存储在名为swarm的数组中)。

public Insect(Tile position, int hp){
    this.position = position;
    this.hp = hp;
    if(position.addInsect(this)){
        position.addInsect(this);
    }
    else{
        throw new IllegalArgumentException("Insect does not fit on tile.");
    }
}

“addInsect”方法检查方块是否能够适合正在其上构建的适当昆虫,如果可以,则将其添加并返回true。因此,我认为可以将其作为if语句的条件来检查是否可以添加,然后如果可以,就执行该方法。但是现在,这似乎运行了两次。当我注释掉if语句,只保留“position.addInsect(this)”时,它似乎会正确运行,只创建一个Hornet。

对于我做错了什么,能提供一些帮助吗?谢谢!

以下是我代码的更多部分:

下面是“addInsect”方法:

public boolean addInsect(Insect insectToAdd){
    if (insectToAdd instanceof HoneyBee) {
        if (this.beeOnTile == null && !this.hornetNestPresence) {
            this.beeOnTile = (HoneyBee) insectToAdd;
            insectToAdd.setPosition(this);
            return true;
        }
        else{
            return false;
        }
    }
    else if (insectToAdd instanceof Hornet){
        if (hornetNestPresence || beeHivePresence || this.isOnThePath()){
            if(this.swarmOnTile != null){
                this.swarmOnTile.addHornet((Hornet) insectToAdd);
                insectToAdd.setPosition(this);
                return true;}
            else{
                this.swarmOnTile = new SwarmOfHornets();
                this.swarmOnTile.addHornet((Hornet) insectToAdd);
                insectToAdd.setPosition(this);
                return true;
            }
        }
        else{
            return false;
        }
    }
    else{
        return false;
    }
}

下面是在成功调用带有新Hornet的addInsect方法时调用的addHornet()方法:

public void addHornet(Hornet newHornet){
    Hornet[] temp_swarm = new Hornet[this.sizeOfSwarm() + 1];

    for (int i = 0; i < this.sizeOfSwarm(); i++){
        temp_swarm[i] = swarm[i];
    }

    temp_swarm[this.sizeOfSwarm()] = newHornet;

    this.swarm = temp_swarm;
    this.swarm_size += 1;
}
英文:

I'm brand new to Java and I am working on an assignment that asks me to set-up a 'tower defense' game with insects. The game has tiles, where before you can create insects and place them on a tile, you have to check whether or not the insect 'fits'.

I'm struggling with setting up a constructor for a subclass Hornet of the abstract class Insect. Currently, every time the following code runs, two new Hornets are placed into the 'Swarm' array instead of one (Hornet objects are stored in an array called swarm).

public Insect(Tile position, int hp){
    this.position = position;
    this.hp = hp;
    if(position.addInsect(this)){
        position.addInsect(this);
    }
    else{
        throw new IllegalArgumentException(&quot;Insect does not fit on tile.&quot;);
    }

}

The 'addInsect' method checks whether or not the tile can fit the appropriate Insect that's being constructed on it, and if it can, it adds it and then returns true. So I thought I could place it as the condition for the if statement to check if it can add, and then if it can, it executes that method. But right now, this seems to be running twice. When I comment out the if statement and only have "position.addInsect(this)", then it seems to run properly and only make one Hornet.

Any help on what I'm doing wrong? Thank you!

Some more of my code in case:

The following is the 'addInsect' method:

    public boolean addInsect(Insect insectToAdd){
    if (insectToAdd instanceof HoneyBee) {
        if (this.beeOnTile == null &amp;&amp; !this.hornetNestPresence) {
            this.beeOnTile = (HoneyBee) insectToAdd;
            insectToAdd.setPosition(this);
            return true;
        }
        else{
            return false;
        }
    }
    else if (insectToAdd instanceof Hornet){
        if (hornetNestPresence || beeHivePresence || this.isOnThePath()){
            if(this.swarmOnTile != null){
                this.swarmOnTile.addHornet((Hornet) insectToAdd);
                insectToAdd.setPosition(this);
                return true;}
            else{
                this.swarmOnTile = new SwarmOfHornets();
                this.swarmOnTile.addHornet((Hornet) insectToAdd);
                insectToAdd.setPosition(this);
                return true;
            }
        }
        else{
            return false;
        }
    }
    else{
        return false;
    }
}

And the following is the addHornet() method that is called when succesfully calling the addInsect method with a new Hornet:

    public void addHornet(Hornet newHornet){
    Hornet[] temp_swarm = new Hornet[this.sizeOfSwarm() + 1];

    for (int i = 0; i &lt; this.sizeOfSwarm(); i++){
        temp_swarm[i] = swarm[i];
    }

    temp_swarm[this.sizeOfSwarm()] = newHornet;

    this.swarm = temp_swarm;
    this.swarm_size += 1;
}

答案1

得分: 3

尝试将构造函数修改如下:

public Insect(Tile position, int hp) {
    this.position = position;
    this.hp = hp;

    if (!position.addInsect(this))
        throw new IllegalArgumentException("Insect does not fit on tile.");
}
英文:

Try to modify your constructor like this:

public Insect(Tile position, int hp) {
    this.position = position;
    this.hp = hp;
    
    if(!position.addInsect(this))
        throw new IllegalArgumentException(&quot;Insect does not fit on tile.&quot;);
}

答案2

得分: 0

我建议将 addInsect 方法拆分为两个不同的方法,一个是检查方法(被动方法),另一个是添加方法(主动方法):

public Insect(Tile position, int hp) {
    this.position = position;
    this.hp = hp;

    if (position.canAddInsect(this))
        position.addInsect(this);
    else
        throw new IllegalArgumentException("Insect does not fit on tile.");
}

public boolean canAddInsect(Insect insectToAdd) {
    if (insectToAdd instanceof HoneyBee)
        return (this.beeOnTile == null && !this.hornetNestPresence);
    else if (insectToAdd instanceof Hornet)
        return (hornetNestPresence || beeHivePresence || this.isOnThePath());

    return false;
}

public void addInsect(Insect insectToAdd) {
    if (insectToAdd instanceof HoneyBee) {
        if (this.beeOnTile == null && !this.hornetNestPresence) {
            this.beeOnTile = (HoneyBee) insectToAdd;
            insectToAdd.setPosition(this);
        }
    } else if (insectToAdd instanceof Hornet) {
        if (hornetNestPresence || beeHivePresence || this.isOnThePath()) {
            if (this.swarmOnTile == null)
                this.swarmOnTile = new SwarmOfHornets();

            this.swarmOnTile.addHornet((Hornet) insectToAdd);
            insectToAdd.setPosition(this);
        }
    }
}
英文:

I would suggest you to separate addInsect into 2 distinct methods, a checker (passive method) and an adder (active method):

public Insect(Tile position, int hp) {
this.position = position;
this.hp = hp;
if(position.canAddInsect(this))
position.addInsect(this);
else
throw new IllegalArgumentException(&quot;Insect does not fit on tile.&quot;);
}
public boolean canAddInsect(Insect insectToAdd) {
if (insectToAdd instanceof HoneyBee)
return (this.beeOnTile == null &amp;&amp; !this.hornetNestPresence);
else if (insectToAdd instanceof Hornet)
return (hornetNestPresence || beeHivePresence || this.isOnThePath());
return false;
}
public void addInsect(Insect insectToAdd) {
if (insectToAdd instanceof HoneyBee) {
if (this.beeOnTile == null &amp;&amp; !this.hornetNestPresence) {
this.beeOnTile = (HoneyBee) insectToAdd;
insectToAdd.setPosition(this);
}
} else if (insectToAdd instanceof Hornet) {
if (hornetNestPresence || beeHivePresence || this.isOnThePath()) {
if (this.swarmOnTile == null)
this.swarmOnTile = new SwarmOfHornets();
this.swarmOnTile.addHornet((Hornet) insectToAdd);
insectToAdd.setPosition(this);
}
}
}

huangapple
  • 本文由 发表于 2020年10月6日 16:22:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64221964.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定