曝光课程

时间:2015-11-01 16:20:33

标签: java class if-statement

Soo,当一个类与其他类一起操作时,我尝试编写代码。以下是#war;" Tim"和#34; Max"等级的巫师。控制台应该说" Now Max hp .. 50"或者"现在蒂姆的马力...... 50马力",但是控制台说了#34;现在最大的马力... 25"或者"现在Tim的hp .. 25 hp" .. 75 - 25 = 50; 100-50 = 50;但是说75 - 25 = 25; 100-50 = 25; 请帮助:c

import java.util.Random;

public class MAXvsTIMvoidVersion {

public static void main(String[] args) {

    warior Tim = new warior();
    Tim.agility = 100;
    Tim.attack = 25;
    Tim.hp = 100;
    Tim.Wariorlvl2 = true;

    witcher Max = new witcher();
    Max.mana = 150;
    Max.attack = 50;
    Max.hp = 75;
    Max.Witcherlvl1 = true;

    Random a = new Random();
    int b = a.nextInt(2);

            if (b == 0)
            {
                Tim.meleeAttack();
                System.out.print("Now Max's hp.." + Max.hp);
            }

            else if (b == 1)
            {
                Max.magicAttack();
                System.out.print("Now Tim's hp.." + Tim.hp);
            }
    }

}


class people
{
    static int hp;
    static int attack;
}

class witcher extends people
{
    int mana;
    boolean Witcherlvl1;
    boolean Witcherlvl2;
    void magicAttack()
    {
        warior.hp = warior.hp - witcher.attack;
        mana = mana - 20;
    }
}

class warior extends people
{
    int agility;
    boolean Wariorlvl1;
    boolean Wariorlvl2;
    void meleeAttack()
    {
        witcher.hp = witcher.hp - warior.attack;
        agility = agility - 20;
    }
}

2 个答案:

答案 0 :(得分:0)

class People
{
    static int hp = 0;
    static int attack = 0;
}

看看上面的代码。它告诉hp和攻击是人类的一部分并且是静态的。因此,Warrior和Witcher从People获得相同的副本[read shared / same]。那么,下面的一段代码

warior Tim = new warior();
Tim.agility = 100;
Tim.attack = 25;
Tim.hp = 100;
Tim.Wariorlvl2 = true;

witcher Max = new witcher();
Max.mana = 150;
Max.attack = 50;
Max.hp = 75;
Max.Witcherlvl1 = true;

通过将攻击设置为50来提供覆盖效果。因此,在meleeAttack()magicAttack()中,您最终会减去50而不是相应的值。

要了解更多信息,请尝试设置Max.attack = 0;

此外,您应该在meleeAttack()magicAttack()中传递受害者的引用,这样可以更清晰。像

这样的东西
void magicAttack( Warrior warrior );

我建议你从People类中删除静态修饰符。这应该是诀窍,Warrior和Witcher最终会得到他们自己的变量副本。

答案 1 :(得分:0)

这个结构怎么样

具有Warrior和Witcher共同特征的人类

class People
{
    int attack;
    int hp;
}

战士班

class Warrior extends People
{

  int agility;
  boolean Warriorlvl2;

  Warrior(int at, int hitp)
  {
    attack = at;
    hp = hitp;
  }

  void meleeAttack(People opponent)
  {
    opponent.hp = opponent.hp - this.attack;
  }
}

巫师班

class Witcher extends People
{
  int mana;
  boolean Witcherlvl1;

  Witcher(int at, int hitp)
 {
    attack = at;
    hp = hitp;
  }

  void magicAttack(People opponent)
  {
    opponent.hp = opponent.hp - this.attack;
  }
}

那么你可以将主类作为

public class MAXvsTIMvoidVersion {

public static void main(String[] args) {

    Warrior Tim = new Warrior(25,100);
    Tim.agility = 100;
    Tim.Wariorlvl2 = true;

    Witcher Max = new Witcher(50,75);
    Max.mana = 150;
    Max.Witcherlvl1 = true;

    Random a = new Random();
    int b = a.nextInt(2);

            if (b == 0)
            {
                Tim.meleeAttack(Max);
                System.out.print("Now Max's hp.." + Max.hp);
            }

            else if (b == 1)
            {
                Max.magicAttack(Tim);
                System.out.print("Now Tim's hp.." + Tim.hp);
            }
    }

}
以这种方式,使用面向对象的方法,实现您想要的行为相当容易。阅读代码并尝试理解。如果你理解困难,我可以指导你。