Java 暴露等级

Java 暴露等级,java,class,if-statement,Java,Class,If Statement,Soo,当一个类与另一个类交互时,我试图编写代码。下面是带“Tim”的班级warior和带“Max”的班级witcher。控制台应显示“现在最大hp..50”或“现在Tim的hp..50 hp”,但控制台显示“现在最大hp..25”或“现在Tim的hp..25 hp”。。75 - 25 = 50; 100-50=50; 但是sayed 75-25=25;100-50=25; 请帮忙:c import java.util.Random; public class MAXvsTIMvoidVers

Soo,当一个类与另一个类交互时,我试图编写代码。下面是带“Tim”的班级warior和带“Max”的班级witcher。控制台应显示“现在最大hp..50”或“现在Tim的hp..50 hp”,但控制台显示“现在最大hp..25”或“现在Tim的hp..25 hp”。。75 - 25 = 50; 100-50=50; 但是sayed 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;
    }
}
请看上面的代码。它告诉我们hp和attack是people类的一部分,是静态的。所以,战士和巫师从人们那里得到了相同的副本。下面是一段代码

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()
magicatack()
中,最后减去50,而不是适当的值

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

此外,您还应该在
meleeAttack()
magicatack()
中传递受害者的引用,这将提供更清晰的信息。差不多

void magicAttack( Warrior warrior );
我建议您从People类中删除静态修饰符。这应该可以做到,就像战士和巫师最终会得到他们自己的变量副本一样

请看上面的代码。它告诉我们hp和attack是people类的一部分,是静态的。所以,战士和巫师从人们那里得到了相同的副本。下面是一段代码

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()
magicatack()
中,最后减去50,而不是适当的值

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

此外,您还应该在
meleeAttack()
magicatack()
中传递受害者的引用,这将提供更清晰的信息。差不多

void magicAttack( Warrior warrior );

我建议您从People类中删除静态修饰符。这应该可以解决问题,因为战士和巫师最终会得到他们自己的变量副本。

这个结构怎么样

具有战士和巫师共同特征的人民阶级

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);
            }
    }

}

这样,使用面向对象的方法,实现您想要的行为就相当容易了。通读代码并试着理解。如果你理解有困难,我可以指导你。

这个结构怎么样

具有战士和巫师共同特征的人民阶级

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);
            }
    }

}

这样,使用面向对象的方法,实现您想要的行为就相当容易了。通读代码并试着理解。如果您在理解上有困难,我可以指导您。

使用OOP处理这个问题不是更好吗?@AbtPst我不太明白您想要表达的意思。问题的核心是删除静态修饰符并允许继承顺利进行,这本身就是OOP功能。是的,我只是认为另一种方法可以以更好的方式构建类。使用OOP来实现这一点不是更好吗?@AbtPst我不太明白您试图传达的内容。问题的核心是删除静态修饰符并允许继承顺利进行,这本身就是OOP功能。是的,我只是认为另一种方法可以更好地构建类。我认为Panan的答案比你的答案简单。但我试过你的cod,这是一个很酷的工作,我只是想展示一下面向对象的方法。快乐编码:)我认为潘南的答案比你的答案简单。但我试过你的cod,这是一个很酷的工作,我只是想展示一下面向对象的方法。快乐编码:)注意:类名以大写字母开头,变量名以小写字母开头。如果您使用这些约定,那么对于普通程序员来说,代码会变得更容易阅读,而且本网站上的代码标记也会以这种方式更好地工作……请注意:类名以大写字母开头,变量名以小写字母开头。如果您使用这些约定,那么对于普通程序员来说,代码会变得更容易阅读,而且这个站点上的代码标记也会以这种方式工作得更好。。。