java - 口袋妖怪课程中调整健康的问题
问题描述
我有两个名为 Pokemon.java 和 Move.java 的类,其中包含创建和修改 Pokemon 及其动作的方法。我已经创建了所有必需的方法,但是我在使用攻击方法时遇到了问题,该方法应该在受到攻击时减去对手的生命值。
这是 Pokemon.java 类的代码:
import java.util.ArrayList;
public class Pokemon
{
// Copy over your code for the Pokemon class here
// Private constants
private static final int MAX_HEALTH = 100;
private static final int MAX_MOVES = 4;
private String name;
private int health;
private int opponentHealth;
public static int numMovesForPokemon = Move.getNumOfMoves();
private Move move;
private static ArrayList<Move> moveListForPokemon = new ArrayList<Move>();
private String pokemonImage;
// Write your Pokemon class here
public Pokemon(String theName, int theHealth)
{
name = theName;
if(theHealth <= MAX_HEALTH)
{
health = theHealth;
}
}
public Pokemon(String name, String image)
{
this.name = name;
health = 100;
pokemonImage = image;
}
public Pokemon(String theName)
{
name = theName;
}
public void setImage(String image)
{
pokemonImage = image;
}
public String getImage()
{
return pokemonImage;
}
public String getName()
{
return name;
}
public int getHealth()
{
return health;
}
public boolean hasFainted()
{
if(health <= 0)
{
return true;
}
else
{
return false;
}
}
public boolean canLearnMoreMoves()
{
if(numMovesForPokemon < 4)
{
return true;
}
else
{
return false;
}
}
public boolean learnMove(Move other)
{
if(canLearnMoreMoves())
{
moveListForPokemon = Move.getList();
moveListForPokemon.add(other);
numMovesForPokemon++;
return true;
}
else
{
return false;
}
}
public void forgetMove(Move other)
{
moveListForPokemon.remove(other);
}
public static ArrayList<Move> displayList()
{
return moveListForPokemon;
}
public boolean knowsMove(Move move)
{
if(moveListForPokemon.contains(move))
{
return true;
}
else
{
return false;
}
}
public boolean knowsMove(String moveName)
{
if(moveListForPokemon.contains(move.getName()))
{
return true;
}
else
{
return false;
}
}
public boolean attack(Pokemon opponent, Move move)
{
if(knowsMove(move))
{
opponentHealth = opponent.getHealth();
opponentHealth -= move.getDamage();
return true;
}
else
{
return false;
}
}
public boolean attack(Pokemon opponent, String moveName)
{
if(knowsMove(moveName))
{
opponentHealth = opponent.getHealth();
opponentHealth -= move.getDamage();
return true;
}
else
{
return false;
}
}
public String toString()
{
return pokemonImage + "\n" + name + " (Health: " + health + " / " + MAX_HEALTH + ")";
}
// Add the methods specified in the exercise description
}
这是 Move.java 类的代码:
import java.util.ArrayList;
public class Move
{
// Copy over your code for the Move class here
private static final int MAX_DAMAGE = 25;
private String name;
private int damage;
public static int numMoves;
private static ArrayList<Move> moveList = new ArrayList<Move>();
public Move(String theName, int theDamage)
{
name = theName;
if(theDamage <= MAX_DAMAGE)
{
damage = theDamage;
}
}
public String getName()
{
return name;
}
public int getDamage()
{
return damage;
}
public static int getNumOfMoves()
{
return numMoves;
}
public static ArrayList<Move> getList()
{
return moveList;
}
public String toString()
{
return name + " (" + damage + " damage)";
}
// Add an equals method so we can compare Moves against each other
public boolean equals(Move other)
{
if(name.equals(other.getName()))
{
return true;
}
else
{
return false;
}
}
}
最后,这是我测试方法的 PokemonTester.java 的代码:
public class PokemonTester extends ConsoleProgram
{
private PokemonImages images = new PokemonImages();
public void run()
{
// Test out your Pokemon class here!
Pokemon p1 = new Pokemon("Charrizard", 100);
Pokemon p2 = new Pokemon("Pikachu", 100);
Move m1 = new Move("Flamethrower", 20);
Move m2 = new Move("Fire Breath", 15);
p1.learnMove(m1);
System.out.println(p1.knowsMove(m1));
System.out.println(p1.knowsMove("Flamethrower"));
System.out.println(p1.attack(p2, m1));
System.out.println(p2.getHealth());
}
}
解决方案
我想你的问题是这样的:
opponentHealth = opponent.getHealth();
opponentHealth -= move.getDamage();
这段代码有几个问题:
- 我建议使用局部变量
opponentHealth
而不是类级别字段 - 对手不知道它的生命值被减去了。您必须与他分享这些知识,例如通过引入 setter for
health
然后调用opponent.setHealth(opponentHealth)
推荐阅读
- meteor - 从 Method 返回的 Meteor Minimongo 插入文档导致订阅错误
- java - 我想将数据库中的多个图像显示到 jsp 中(我正在映射 servlet),所以在 jsp m 中将显示在 img 标签的 src 中
- c++ - C++链表用户输入越界验证导致程序结束
- python - 不同的方法在 Django 中无法正常工作
- javascript - 如何修复 HasAttributes.php 中的 ErrorException
- ionic-framework - Ionic 4:我正在使用 Document Viewer 查看 pdf 文件。但它不工作
- android - 用于使用 Picasso 显示来自 URL 的图像的绑定适配器
- python - 我正在尝试创建一个对象,但应该工作的代码不起作用
- android-studio - 如果没有明确的 null 检查,编译器将无法识别不可为 null 的值
- html - CSS :active 仅在点击期间有效