java宠物小精灵,简单的Java口袋妖怪扑灭模拟器
我寫(xiě)了一個(gè)類(lèi)來(lái)創(chuàng)建和戰(zhàn)斗口袋妖怪,但我無(wú)法弄清楚如何在測(cè)試器類(lèi)中調(diào)用battle方法來(lái)測(cè)試我寫(xiě)的類(lèi).
我的任務(wù)是編寫(xiě)和測(cè)試模擬兩個(gè)神奇寶貝之間的戰(zhàn)斗的模擬.每個(gè)神奇寶貝都有一個(gè)健康值,一個(gè)力量值和一個(gè)速度值.運(yùn)行狀況,強(qiáng)度和速度值作為參數(shù)傳遞給構(gòu)造函數(shù).這些值最初必須介于1到300之間,并且最初應(yīng)為非零值.完成游戲的總體思路是兩個(gè)口袋妖怪將在模擬中相互“戰(zhàn)斗”,口袋妖怪輪流攻擊. (具有最高速度值的那一個(gè)每輪首先出現(xiàn))攻擊口袋妖怪的力量將從“攻擊者”的生命值中減去.
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");
}
}
口袋妖怪測(cè)試員
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
}
}
我確實(shí)意識(shí)到我還沒(méi)有在輪流中實(shí)現(xiàn)速度方面,因?yàn)槲艺噲D讓它工作.
添加靜態(tài)到戰(zhàn)斗功能,就像在main中一樣.
此外,你不能在主要使用charizard和blastoise.非靜態(tài)變量不能用于靜態(tài)函數(shù).您需要在`main中創(chuàng)建局部變量
public static void main(String[] args){
Pokemon charizard = new Pokemon(100,50,50);
Pokemon blastoise = new Pokemon(150,25,150);
Pokemon.battle(charizard, blastoise);
}
您還可以創(chuàng)建新的PokemonTester并使用它的變量:
public static void main(String[] args){
PokemonTester tester=new PokemonTester();
Pokemon.battle(tester.charizard, tester.blastoise);
}
您可以了解有關(guān)靜態(tài)成員here的更多信息
總結(jié)
以上是生活随笔為你收集整理的java宠物小精灵,简单的Java口袋妖怪扑灭模拟器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: php pdf 加密 签名 时间戳,在现
- 下一篇: 芙蓉花叶的功效与作用、禁忌和食用方法