2016-09-25 51 views
-1

我正在做一个有趣的宠物小精灵游戏,我希望能够降低两个宠物小精灵战斗的HP。我在做的是在一个'if语句'内部调用一个方法,这个方法在一个循环内部,让Java调用另一个类的方法来减少HP。对象互动ñJava

下面是代码,因为我有它...

import java.util.Scanner; 

public class gameTester { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     Scanner inputSystem = new Scanner(System.in); 
     Scanner playerName = inputSystem; 


     System.out.println("Hello Player please type in the name of your pokemon."); 

     pokemon playerOne = new pokemon(playerName.nextLine()); 
     pokemon playerTwo = new pokemon(); 

     System.out.println(playerOne.toString());//shows player pokemon 
     System.out.println(playerTwo.toString());//shows enemy pokemon 

     System.out.println("Let's Battle! What do you do?"); 

     while (playerOne.getHealthPoints() >= 0 || playerTwo.getHealthPoints() >= 0){ 
     System.out.println("1. Bite 2. Slash 3. Flee"); 
     int userChoice = inputSystem.nextInt(); 
      if (userChoice == 3){ 
      break; 
      } 
      else if (userChoice == 1 || userChoice == 2){ 
       //playerTwo.getHealthPoints() 
      } 
     } 
    } 
} 

而且就像我上面说我打电话从其他类中的方法..

public class pokemon { 

    private String pokemonSpecies; 
    private String nameOfpokemon; 
    private int attackDamage; 
    private int healthPoints; 

    public pokemon(){ 
     nameOfpokemon = "Enemy"; 
     attackDamage = 1; 
     healthPoints = 3; 
    } 
    public pokemon (String desiredName){ 
     nameOfpokemon = desiredName; 
     attackDamage = 1; 
     healthPoints = 3; 
    } 
    public String getPokemonSpecies() { 
     return pokemonSpecies; 
    } 
    public void setPokemonSpecies(String pokemonSpecies) { 
     this.pokemonSpecies = pokemonSpecies; 
    } 
    public String getNameOfpokemon() { 
     return nameOfpokemon; 
    } 
    public void setNameOfpokemon(String nameOfpokemon) { 
     this.nameOfpokemon = nameOfpokemon; 
    } 
    public int getAttackDamage() { 
     return attackDamage; 
    } 
    public void setAttackDamage(int attackDamage) { 
     this.attackDamage = attackDamage; 
    } 
    public int getHealthPoints() { 
     return healthPoints; 
    } 
    public void setHealthPoints() { 
     this.healthPoints = healthPoints; 
    } 
    @Override 
    public String toString(){ 
     return "Name of Pokemon: " + nameOfpokemon + " Attack Damage: " + attackDamage + " Health Points: " + healthPoints; 
    } 
    public int enemyDamage(int damage){ 
     setHealthPoints() = getAttackDamage() - getHealthPoints(); 
    } 
} 

对公众的最后一位在enemyDamage(...)是我卡住的地方。我不知道是否应该发送一个可以减少HP的整数。或者我应该用这种方法来调用其他方法...

有什么建议吗?

+0

方法'enemyDamage'中的行可能不是有效的Java。 想想你想要发生什么; '这需要造成伤害'等等。 – Entalpi

+0

将'setHealthPoints()= getAttackDamage() - getHealthPoints()'改为'setHealthPoints(getHealthPoints() - getAttackDamage())'并更改setter的签名。二传手必须接收一个参数来做任何明智的事情。 – HopefullyHelpful

回答

1

首次使用可以在setHealthPoints()方法更改为

public void setHealthPoints(int healthPoints) { 
    this.healthPoints = healthPoints; 
} 

在这里,我承担起对手的小宠物做

伤害=攻击/损坏。

getHealthPoints()=我的宠物小精灵的健康。

Then enemyDamage()就是这样。

public void enemyDamage(int damage){ 
    setHealthPoints(getHealthPoints() - damage); 
} 
+0

第二种方法你回来了什么?你是否返回'损害'或其他什么? – 3man75

+0

更新!谢谢。事实上,它不应该返回任何东西,因为它只会在损坏后设置健康点。你可以通过'getHealthPoints()'得到健康' – Shaggy

+0

似乎可以工作,但是伤害会保存在小宠物healhpoints字段中。 – 3man75