我写了一个类来创建和战斗口袋妖怪,但我无法弄清楚如何在测试器类中调用war方法来测试我写的类。
我的任务是编写和测试模拟两个神奇宝贝之间的战斗的模拟。每个神奇宝贝都有一个健康值,一个力量值和一个速度值。运行状况,强度和速度值作为参数传递给构造函数。这些值最初必须介于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
}
}
我确实意识到我还没有在轮流方面实施速度方面,因为我正努力让它发挥作用。
答案 0 :(得分:6)
将static
添加到battle
功能,就像在main
中一样。
此外,您无法在charizard
中使用blastoise
和main
。非静态变量不能用于静态函数。您需要在`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
答案 1 :(得分:1)
对我来说很好。我唯一能说的代码是你的战斗方法不是静态的。
public static void battle(Pokemon pokemon1, Pokemon pokemon2)
答案 2 :(得分:0)
除了将战斗功能设为静态之外,我还建议进行以下更改。 当我运行您的代码时,我得到:
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");
}
}