首页 > 解决方案 > 在带有对象的arrayList中查找字符串的问题

问题描述

我有两个名为 Pokemon.java 和 Move.java 的类,其中包含创建和修改 Pokemon 及其动作的方法。我已经创建了所有必需的方法,但是我对 knowMove 方法有疑问,特别是需要 aString而不是 aMove方法。该方法应该查看具有给定字符串的移动是否在移动列表中。当我测试该方法时,它在应该返回 false 时返回 true,因为我给它的字符串没有移动。

这是 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 int doesListContainName = 0;
    private Move move;
    private 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 void setHealth(int health)
    {
        this.health = 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.add(other);
            numMovesForPokemon++;
            return true;
        }
        else
        {
            return false;
        }
    }

    public void forgetMove(Move other)
    {
            moveListForPokemon.remove(other);
    }

    public ArrayList<Move> displayList()
    {
        return moveListForPokemon;
    }

    public boolean knowsMove(Move move)
    {
        if(moveListForPokemon.contains(move))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean knowsMove(String moveName)
    {
        for(Move m : moveListForPokemon)
        {
            if(m.getName() != null && m.getName().contains(moveName))
            {
                doesListContainName = 1;
            }

        }

        if(doesListContainName == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean attack(Pokemon opponent, Move move)
    {
        if(knowsMove(move))
        {
            opponentHealth = opponent.getHealth();
            opponentHealth -= move.getDamage();
            opponent.setHealth(opponentHealth);
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean attack(Pokemon opponent, String moveName)
    {
        if(knowsMove(moveName))
        {
            opponentHealth = opponent.getHealth();
            opponentHealth -= move.getDamage();
            opponent.setHealth(opponentHealth);
            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 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 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("Pokemon knows move 1: " + p1.knowsMove(m1));
        System.out.println("Pokemon knows move 1 (String): " + p1.knowsMove("Flamethrower"));
        System.out.println("Pokemon knows move 2 (String): " + p1.knowsMove("Fire Breath"));
        System.out.println("Pokemon knows move 2: " + p1.knowsMove(m2));
        System.out.println("Move 1 Damage: " + m1.getDamage());
        System.out.println("Pokemon attack opponent with move 1: " + p1.attack(p2, m1));
        System.out.println("Opponent health: " + p2.getHealth());
    }
}

标签: java

解决方案


dosListContainName 不应是类变量,而应是“knowsMo​​ve”方法的局部变量。变量 dosListContainName 初始化为 0,然后测试调用“knowsMo​​ve”并使用它知道的移动,因此将 dosListContainName 变量设置为 1。然后测试使用未知移动调用 knowMove,但 dosListContainName 仍然为 1,因此它返回也是如此。


推荐阅读