Simple java Pokemon extinguishing simulator

I wrote a class to create and fight Pokemon, but I couldn't figure out how to call the battle method in the tester class to test the class I wrote.

My task is to write and test a simulation that simulates the battle between two magic babies Every magic baby has a health value, a strength value and a speed value Health, strength and speed values are passed to the constructor as parameters These values must initially be between 1 and 300 and should initially be non-zero The general idea of completing the game is that the two Pokemon will "fight" each other in the simulation, and the Pokemon will attack in turn (the one with the highest speed value appears first in each round) the power to attack Pokemon will be subtracted from the attacker's health

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

Pokemon tester

public class PokemonTester{
  private Pokemon charizard;
  private Pokemon blastoise;
  private Pokemon venusaur;

public PokemonTester(){
   charizard = new Pokemon(100,50,50);
   blastoise = new Pokemon(150,25,150);
   venusaur = new Pokemon(300,10,100);
 }

public static void main(String[] args){
  Pokemon.battle(charizard,blastoise); //will not compile
 }
}

I do realize that I haven't achieved speed in rotation because I'm trying to make it work

Solution

Add static to the combat function, just like in main

In addition, you can't use Charizard and blastoise Non static variables cannot be used in static functions You need to create local variables in ` main

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

You can also create a new Pokemon tester and use its variables:

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

You can learn more about the static member here

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>