用Java创建神奇宝贝类

时间:2020-01-16 00:04:46

标签: java

我有一个Java项目,要求我有两个名为Pokemon.java和Move.java的类,因此我可以添加新的Pokemon及其动作。我已经写了项目所需的所有方法,但是在存储和修改动作时遇到了问题,特别是Pokemon类中的忘记移动方法。

这是Pokemon.java的代码:

    public class Pokemon
{
    // Private constants
    private static final int MAX_HEALTH = 100;
    private static final int MAX_MOVES = 4;
    private String name;
    private int health;
    private Move move;

    // Write your Pokemon class here
    public Pokemon(String theName, int theHealth)
    {
        name = theName;
        if(theHealth <= MAX_HEALTH)
        {
            health = theHealth;
        }
    }

    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(Move.getNumOfMoves() < 4)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean learnMove(Move move)
    {
        if(canLearnMoreMoves())
        {
            this.move = move;
            return true;
        }
        else
        {
            return false;
        }
    }

    public void forgetMove(Move other)
    {
        if(Move.equals(other))
        {
            move -= other;
        }
    }

    public String toString()
    {
        return name + " (Health: " + health + " / " + MAX_HEALTH + ")";
    }
}

这是Move.java的代码:

public class Move
{
    // Copy over your Move class into here
    private static final int MAX_DAMAGE = 25;
    private static String name;
    private static int damage;
    public static int numMoves;

    public Move(String theName, int theDamage)
    {
        name = theName;
        if(theDamage <= MAX_DAMAGE)
        {
            damage = theDamage;
        }
        numMoves++;
    }

    public static String getName()
    {
        return name;
    }

    public static int getDamage()
    {
        return damage;
    }

    public static int getNumOfMoves()
    {
        return numMoves;
    }

    public String toString()
    {
        return name + " (" + damage + " damage)";
    }    
    // Add an equals method so we can compare Moves against each other

    public static boolean equals(Move other)
    {
        if(name.equals(other.getName()))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

这是PokemonTester.java的代码:

public class PokemonTester extends ConsoleProgram
{
    public void run()
    {
        Pokemon p1 = new Pokemon("Charrizard", 100);
        Move m1 = new Move("Flamethrower", 90);
        System.out.println(p1);
        System.out.println(m1);
    }
}

1 个答案:

答案 0 :(得分:2)

I won't give you a full implementation似乎是一项家庭作业。

如果您只是填写Pokemon和Move类所需的方法,那么我将从重新考虑存储移动的方式开始。

getNumOfMoves提示您的Pokemon类应该存储多个动作,一种常见的实现方法是使用arrayslists

如果您将移动存储在列表中,则忘记移动功能可能如下所示:

public void forgetMove(Move other){
  moves.remove(other);
}
相关问题