我已经编写了一个类来创建和与pokemon战斗,但是我不知道如何调用tester类中的battle方法来测试我编写的类。
我的攻击是编写和测试一个模拟两个口袋妖怪之间的战斗的模拟。每个Pokemon都有一个健康值、一个力量值和一个速度值。健康、强度和速度值作为参数传递给构造函数。这些值最初必须介于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");
}
}精灵宝可梦测试仪
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
}
}我确实意识到我还没有在轮流中实现速度方面,因为我只是试图让它工作。
发布于 2011-10-30 07:44:51
将static添加到battle函数中,就像在main中一样。
此外,您不能在main中使用charizard和blastoise。不能在静态函数中使用非静态变量。您需要在` `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);
}您可以了解有关静态成员here的更多信息
发布于 2016-11-03 05:51:04
它对我来说很好。我唯一要说的是代码的错误是你的战斗方法不是静态的。
public static void battle(Pokemon pokemon1, Pokemon pokemon2)发布于 2019-12-27 18:34:08
除了使战斗函数成为静态函数外,我还建议进行以下更改。当我运行你的代码时,我得到:
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都不是的好名字。要更改这一点,请向构造函数添加一个字符串类型的name字段,并相应地使用它。战斗模拟显然是错误的,因为(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");
}
}https://stackoverflow.com/questions/7942384
复制相似问题