admin管理员组

文章数量:1531907

2023年12月13日发(作者:)

口袋妖怪java_简单的Java口袋妖怪扑灭模拟器

我已经写了一个类创建和战斗口袋妖怪,但我不知道如何调用测试类中的战斗方法,以测试我写的类。简单的Java口袋妖怪扑灭模拟器

我的任务是编写和测试模拟两个口袋妖怪之间的战斗模拟。每个口袋妖怪都有一个健康值,一个强度值和一个速度值。健康值,强度值和速

度值作为参数传递给构造函数。这些值最初必须介于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;

= health;

th = strength;

= speed;

}

public void battle(Pokemon pokemon1, Pokemon pokemon2){

do{

n(pokemon1+" begins the fight against "+pokemon2);

= - th;

n(pokemon1 +" does "+ th +" damage to "+

pokemon2 +" and "+ pokemon2 +" has "+ +" left.");

= - th;n(pokemon2 +" does "+ th +" damage to "+

pokemon1 +" and "+ pokemon1 +" has "+ +" left.");

}while( >= 1 || >= 1);

if( < 1)

n(pokemon1 +" has lost the fight");

else

n(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){

(charizard, blastoise); //will not compile

}

}

我意识到我还没有实现在服用速度方面还转,因为我想只是使它工作。

2011-10-29

Brian

+1

请注意,你的while可能不是你想要的:只要一个口袋妖怪的健康<= 0,while循环应该停止。因此,你需要一个'while > = 1

&& > = 1'注意'&&'而不是'||' –

+2

从现在开始,我将使用口袋妖怪名称作为变元变量。 –

+0

@Inerdia我同意这比真正的动物编程101锻炼更有趣 - 嘿,谁想看到狗与猫战斗? ;) –

本文标签: 口袋妖怪战斗测试速度攻击