简单Java口袋妖怪战斗模拟器

简单Java口袋妖怪战斗模拟器,java,Java,我已经编写了一个类来创建并与口袋妖怪战斗,但是我不知道如何在tester类中调用battle方法来测试我编写的类 我的任务是编写和测试一个模拟两个口袋妖怪之间战斗的模型。每个口袋妖怪都有一个健康值、一个力量值和一个速度值。运行状况、强度和速度值作为参数传递给构造函数。这些值最初必须介于1和300之间,并且最初应为非零。完成游戏的总体思路是,两个口袋妖怪将在模拟中相互“战斗”,由口袋妖怪轮流攻击。(每轮速度值最高的一个会先走一步)攻击口袋妖怪的力量将从“攻击者”的生命值中减去 public cla

我已经编写了一个类来创建并与口袋妖怪战斗,但是我不知道如何在tester类中调用battle方法来测试我编写的类

我的任务是编写和测试一个模拟两个口袋妖怪之间战斗的模型。每个口袋妖怪都有一个健康值、一个力量值和一个速度值。运行状况、强度和速度值作为参数传递给构造函数。这些值最初必须介于1和300之间,并且最初应为非零。完成游戏的总体思路是,两个口袋妖怪将在模拟中相互“战斗”,由口袋妖怪轮流攻击。(每轮速度值最高的一个会先走一步)攻击口袋妖怪的力量将从“攻击者”的生命值中减去

public class Pokemon{
  private int health;
  private int strength;
  private int speed;

/**
* Constructs the pokemon
* @Require:
*    health is an integer greater than or equal to 1 but less than or equal to 300
*    strength is and integer greater than or equal to 1 but less than or equal to 300
*    speed is an integer greater than or equal to 1 but less than or equal to 300
*/
public Pokemon(int health, int strength, int speed){
  assert health >= 1;
  assert health <= 300;
  assert strength >= 1;
  assert strength <= 300;
  assert speed >= 1;
  assert speed <= 300;

  this.health = health;
  this.strength = strength;
  this.speed = speed;
}

public void battle(Pokemon pokemon1, Pokemon pokemon2){
  do{
    System.out.println(pokemon1+" begins the fight against "+pokemon2);
    pokemon2.health = pokemon2.health - pokemon1.strength;

    System.out.println(pokemon1 +" does "+ pokemon1.strength +" damage to "+
    pokemon2 +" and "+ pokemon2 +" has "+ pokemon2.health +" left.");

    pokemon1.health = pokemon1.health - pokemon2.strength;

    System.out.println(pokemon2 +" does "+ pokemon2.strength +" damage to "+ 
    pokemon1 +" and "+ pokemon1 +" has "+ pokemon1.health +" left.");

  }while(pokemon1.health >= 1 || pokemon2.health >= 1);
  if(pokemon1.health < 1)
    System.out.println(pokemon1 +" has lost the fight");
  else
    System.out.println(pokemon2 +" has lost the fight");
  }
}

我确实意识到我还没有在轮换中实现速度方面,因为我只是想让它工作。

静态
添加到
战斗
功能中,就像在
中一样

此外,您不能在
main
中使用
charizard
blanoise
。非静态变量不能在静态函数中使用。您需要在“main”中创建局部变量

public static void main(String[] args){
    Pokemon charizard = new Pokemon(100,50,50);
    Pokemon blastoise = new Pokemon(150,25,150);
    Pokemon.battle(charizard, blastoise);
}
您还可以创建新的
PokemonTester
,并使用其变量:

public static void main(String[] args){
    PokemonTester tester=new PokemonTester();
    Pokemon.battle(tester.charizard, tester.blastoise);
}

您可以了解更多关于静态成员的信息

这对我来说很好。我唯一要说的是,代码的错误在于,您的作战方法不是静态的

public static void battle(Pokemon pokemon1, Pokemon pokemon2)

除了使battle函数保持静态外,我建议进行以下更改。 当我运行您的代码时,我得到:

Pokemon@6228a17f begins the fight against Pokemon@c9be777
Pokemon@6228a17f does 50 damage to Pokemon@c9be777 and Pokemon@c9be777 has 100 left.
Pokemon@c9be777 does 25 damage to Pokemon@6228a17f and Pokemon@6228a17f has 75 left.
Pokemon@6228a17f begins the fight against Pokemon@c9be777
Pokemon@6228a17f does 50 damage to Pokemon@c9be777 and Pokemon@c9be777 has 50 left.
Pokemon@c9be777 does 25 damage to Pokemon@6228a17f and Pokemon@6228a17f has 50 left.
Pokemon@6228a17f begins the fight against Pokemon@c9be777
Pokemon@6228a17f does 50 damage to Pokemon@c9be777 and Pokemon@c9be777 has 0 left.
Pokemon@c9be777 does 25 damage to Pokemon@6228a17f and Pokemon@6228a17f has 25 left.
Pokemon@6228a17f begins the fight against Pokemon@c9be777
Pokemon@6228a17f does 50 damage to Pokemon@c9be777 and Pokemon@c9be777 has -50 left.
Pokemon@c9be777 does 25 damage to Pokemon@6228a17f and Pokemon@6228a17f has 0 left.
Pokemon@6228a17f has lost the fight

名称 @6228a17f和@6228a17f不是口袋妖怪的好名字。要更改此设置,请向构造函数添加字符串类型名称字段,并相应地使用它。 战斗 这场战斗的模拟显然是错误的 (a) 战斗每回合“开始” (b) 当口袋妖怪的生命值降到0或以下时,战斗不会停止。 为了解决这个问题,我建议使用条目控制的while循环,而不是do-while,并在第一轮之后插入break语句。应用这些更改,您的程序将执行以下操作:
Pokemon:

public class Pokemon{
    private int health;
    private int strength;
    private int speed;
    private String name;
    public Pokemon(String name, int health, int strength, int speed){
        assert health >= 1;
        assert health <= 300;
        assert strength >= 1;
        assert strength <= 300;
        assert speed >= 1;
        assert speed <= 300;
        this.health = health;
        this.strength = strength;
        this.speed = speed;
        this.name = name;
    }

    public static void battle(Pokemon pokemon1, Pokemon pokemon2)
    {
        System.out.println(pokemon1.name+" begins the fight against "+pokemon2.name);
        while
        (pokemon1.health >= 1 || pokemon2.health >= 1)
        {

            pokemon2.health = pokemon2.health - pokemon1.strength;

            System.out.println(pokemon1.name +" does "+ pokemon1.strength +" damage to "+
                pokemon2.name +" and "+ pokemon2.name +" has "+ pokemon2.health +" health left.");

            if
            (pokemon2.health <= 0)
            break;

            pokemon1.health = pokemon1.health - pokemon2.strength;

            System.out.println(pokemon2.name +" does "+ pokemon2.strength +" damage to "+ 
                pokemon1.name +" and "+ pokemon1.name +" has "+ pokemon1.health +" health left.");

        }

        if
        (pokemon1.health < 1)
            System.out.println(pokemon1.name +" has lost the fight");
        else
            System.out.println(pokemon2.name +" has lost the fight");
    }
}
公共类口袋妖怪{
私人卫生;
私人综合实力;
私人整数速度;
私有字符串名称;
公共口袋妖怪(字符串名称、整数生命值、整数强度、整数速度){
断言健康>=1;
断言健康=1;
断言强度=1;
断言速度=1 | | pokemon2.health>=1)
{
pokemon2.health=pokemon2.health-pokemon1.strength;
System.out.println(pokemon1.name+“does”+pokemon1.strength+“damage to”+
pokemon2.name+”和“+pokemon2.name+”有“+pokemon2.health+”健康离开。”);
如果

(pokemon2.health注意,你的while可能不是你想要的:只要一个pokemon拥有health=1&&pok2.health>=1
注意
&&
而不是
|
,从现在起,我将使用pokemon名称作为元语法变量。@Inerdia我同意这比使用re进行101编程要有趣得多所有动物-见鬼,谁会想看到狗和猫打架?;)如果我尝试,编译器会给我一个“错误:非静态方法战(口袋妖怪,口袋妖怪)不能从静态上下文引用”@Brian你需要在你的
battle
函数中添加
static
修饰符。好的,谢谢,它现在符合并运行了,但是输出没有给出口袋妖怪的“名称”,而是它的名称标识符,即。pokemon@1ee1dea2.如何修复此问题?@Brian变量名仅在编译时已知。您需要将
String添加到String()中
方法到您的
Pokemon
类。此方法应返回带有您的Pokemon名称的
String
。您能给我一个示例吗?
public class Pokemon{
    private int health;
    private int strength;
    private int speed;
    private String name;
    public Pokemon(String name, int health, int strength, int speed){
        assert health >= 1;
        assert health <= 300;
        assert strength >= 1;
        assert strength <= 300;
        assert speed >= 1;
        assert speed <= 300;
        this.health = health;
        this.strength = strength;
        this.speed = speed;
        this.name = name;
    }

    public static void battle(Pokemon pokemon1, Pokemon pokemon2)
    {
        System.out.println(pokemon1.name+" begins the fight against "+pokemon2.name);
        while
        (pokemon1.health >= 1 || pokemon2.health >= 1)
        {

            pokemon2.health = pokemon2.health - pokemon1.strength;

            System.out.println(pokemon1.name +" does "+ pokemon1.strength +" damage to "+
                pokemon2.name +" and "+ pokemon2.name +" has "+ pokemon2.health +" health left.");

            if
            (pokemon2.health <= 0)
            break;

            pokemon1.health = pokemon1.health - pokemon2.strength;

            System.out.println(pokemon2.name +" does "+ pokemon2.strength +" damage to "+ 
                pokemon1.name +" and "+ pokemon1.name +" has "+ pokemon1.health +" health left.");

        }

        if
        (pokemon1.health < 1)
            System.out.println(pokemon1.name +" has lost the fight");
        else
            System.out.println(pokemon2.name +" has lost the fight");
    }
}