我正在做一個有趣的寵物小精靈遊戲,我希望能夠降低兩個寵物小精靈戰鬥的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的整數。或者我應該用這種方法來調用其他方法...
有什麼建議嗎?
方法'enemyDamage'中的行可能不是有效的Java。 想想你想要發生什麼; '這需要造成傷害'等等。 – Entalpi
將'setHealthPoints()= getAttackDamage() - getHealthPoints()'改爲'setHealthPoints(getHealthPoints() - getAttackDamage())'並更改setter的簽名。二傳手必須接收一個參數來做任何明智的事情。 – HopefullyHelpful