2016-09-25 50 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