首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法调用Pokemon类中的对象

无法调用Pokemon类中的对象
EN

Stack Overflow用户
提问于 2020-01-20 10:16:08
回答 1查看 84关注 0票数 0

我有三个名为Pokemon.java,Move.java和PokemonTrainer.java的类,它们包含了创建和修改口袋妖怪的方法,它们的动作和它们的训练器。我已经创建了所有必需的方法,但在调用我在PokemonSimulation.java类中创建的精灵宝可梦对象时遇到问题,程序总是无法编译,并显示错误"java:56: error: cannot find symbol“。我该如何解决这个问题呢?

以下是Pokemon.java的代码:

代码语言:javascript
复制
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 ArrayList<Move> moveListForPokemon;
    private String pokemonImage;

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

    public Pokemon(String name, String image)
    {
        this.name = name;
        health = 100;
        pokemonImage = image;
    }

    public Pokemon(String theName)
    {
        name = theName;
        health = 100;
        pokemonImage = 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> getMoves()
    {
        return moveListForPokemon;
    }

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

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

        }

        if(doesListContainName == 1)
        {
            doesListContainName = 0;
            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();
            for(Move m : moveListForPokemon)
            {
                if(m.getName() != null && m.getName().equals(moveName))
                {
                    opponentHealth -= m.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的代码:

代码语言:javascript
复制
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;
        }
    }     
}

以下是PokemonTrainer.java的代码:

代码语言:javascript
复制
import java.util.ArrayList;
public class PokemonTrainer
{
    // private constants
    private static final int MAX_POKEMON = 2;
    private ArrayList<Pokemon> pokemonList = new ArrayList<Pokemon>();
    private String name;
    private Pokemon p;
    private int numOfPokemon;

    // Write your PokemonTrainer class here
    public PokemonTrainer(String name)
    {
        this.name = name;
    }

    public boolean addPokemon(Pokemon p)
    {
        if(numOfPokemon < MAX_POKEMON)
        {
            pokemonList.add(p);
            numOfPokemon++;
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean hasLost()
    {
        int numOfPokemonThatLost = 0;
        for(Pokemon p : pokemonList)
        {
            if(p.hasFainted())
            {
                numOfPokemonThatLost++;
            }
        }
        if(numOfPokemonThatLost == numOfPokemon)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public Pokemon getNextPokemon()
    {
        int nextPokemon = 0;
        int numOfPokemonThatLost = 0;
        for(Pokemon p : pokemonList)
        {
            while(p.hasFainted() && nextPokemon < numOfPokemon)
            {
                nextPokemon++;
                numOfPokemonThatLost++;
                if(nextPokemon < numOfPokemon)
                {
                    p = pokemonList.get(nextPokemon);
                }
            }
        }
        if(numOfPokemonThatLost == numOfPokemon)
        {
            return null;
        }
        else
        {
            return pokemonList.get(nextPokemon);
        }
    }

    public String toString()
    {
        return name;
    }
}

最后,下面是PokemonSimulation.java的代码:

代码语言:javascript
复制
public class PokemonSimulation extends ConsoleProgram
{
    private PokemonImages images = new PokemonImages();
    private String trainerName;
    private String trainerName2;
    private String pokemonImage;
    private String pokemonName;
    private String newMove;
    private String moveName;
    private int moveDamage;
    private int numOfMoves1;
    private int numOfMoves2;
    private int numOfMoves3;
    private int numOfMoves4;

    public void run()
    {
        System.out.println("Welcome the Pokemon Simulator!");
        System.out.println("");
        // First trainer
        System.out.println("Set up first Pokemon Trainer:");
        trainerName = readLine("Trainer, what is your name? ");
        PokemonTrainer pt1 = new PokemonTrainer(trainerName);
        System.out.println("Hello " + pt1 + "!");
        System.out.println("");
        System.out.println("Choose your first pokemon");
        pokemonName = readLine("Enter the name of your pokemon: ");
        System.out.println("You chose:");
        if(pokemonName.equalsIgnoreCase("Pikachu") || pokemonName.equalsIgnoreCase("Charmander") || pokemonName.equalsIgnoreCase("Bulbasaur") || pokemonName.equalsIgnoreCase("Squirtle"))
        {
            Pokemon p1 = new Pokemon(pokemonName, images.getPokemonImage(pokemonName));
            System.out.println(p1);
        }
        else
        {
            Pokemon p1 = new Pokemon(pokemonName);
            System.out.println(p1);
        }
        System.out.println("");
        newMove = readLine("Would you like to teach " + pokemonName +  " a new move? ");
        if(newMove.equalsIgnoreCase("yes"))
        {
            while(newMove.equalsIgnoreCase("yes") && numOfMoves1 < 4)
            {
                moveName = readLine("Enter the name of the move: ");
                moveDamage = readInt("How much damage does this move do? ");
                if(numOfMoves1 == 0)
                {
                    Move m1 = new Move(moveName, moveDamage);
                    p1.learnMove(m1);
                    System.out.println(pokemonName + " learned " + m1);
                }
                else if(numOfMoves1 == 1)
                {
                    Move m2 = new Move(moveName, moveDamage);
                    p1.learnMove(m2);
                    System.out.println(pokemonName + " learned " + m2);
                }
                else if(numOfMoves1 == 2)
                {
                    Move m3 = new Move(moveName, moveDamage);
                    p1.learnMove(m3);
                    System.out.println(pokemonName + " learned " + m3);
                }
                else if(numOfMoves1 == 3)
                {
                    Move m4 = new Move(moveName, moveDamage);
                    p1.learnMove(m4);
                    System.out.println(pokemonName + " learned " + m4);
                }
                numOfMoves1++;
                System.out.println("");
                newMove = readLine("Would you like to teach " + pokemonName +  " a new move? ");
                if(newMove.equalsIgnoreCase("yes") && numOfMoves1 < 4)
                {
                }
                else if(newMove.equalsIgnoreCase("yes") && numOfMoves1 >= 4)
                {
                    System.out.println("Sorry, you can't teach " + pokemonName + " anymore more moves.");
                    System.out.println("");
                    break;
                }
                else
                {
                    System.out.println(pokemonName + " has learned all of their moves");
                    System.out.println("");            
                }
            }
        }
        else
        {
            System.out.println(pokemonName + " has learned all of their moves");
            System.out.println("");
        }
        System.out.println("Choose your second pokemon");
        pokemonName = readLine("Enter the name of your pokemon: ");
        System.out.println("You chose:");
        if(pokemonName.equalsIgnoreCase("Pikachu") || pokemonName.equalsIgnoreCase("Charmander") || pokemonName.equalsIgnoreCase("Bulbasaur") || pokemonName.equalsIgnoreCase("Squirtle"))
        {
            Pokemon p2 = new Pokemon(pokemonName, images.getPokemonImage(pokemonName));
            System.out.println(p2);
        }
        else
        {
            Pokemon p2 = new Pokemon(pokemonName);
            System.out.println(p2);
        }
        System.out.println("");
        newMove = readLine("Would you like to teach " + pokemonName +  " a new move? ");
        if(newMove.equalsIgnoreCase("yes"))
        {
            while(newMove.equalsIgnoreCase("yes") && numOfMoves2 < 4)
            {

                moveName = readLine("Enter the name of the move: ");
                moveDamage = readInt("How much damage does this move do? ");
                if(numOfMoves2 == 0)
                {
                    Move m5 = new Move(moveName, moveDamage);
                    p2.learnMove(m5);
                    System.out.println(pokemonName + " learned " + m5);
                }
                else if(numOfMoves2 == 1)
                {
                    Move m6 = new Move(moveName, moveDamage);
                    p2.learnMove(m6);
                    System.out.println(pokemonName + " learned " + m6);
                }
                else if(numOfMoves2 == 2)
                {
                    Move m7 = new Move(moveName, moveDamage);
                    p2.learnMove(m7);
                    System.out.println(pokemonName + " learned " + m7);
                }
                else if(numOfMoves2 == 3)
                {
                    Move m8 = new Move(moveName, moveDamage);
                    p2.learnMove(m8);
                    System.out.println(pokemonName + " learned " + m8);
                }
                numOfMoves2++;
                System.out.println("");
                newMove = readLine("Would you like to teach " + pokemonName +  " a new move? ");
                if(newMove.equalsIgnoreCase("yes") && numOfMoves2 < 4)
                {
                }
                else if(newMove.equalsIgnoreCase("yes") && numOfMoves2 >= 4)
                {
                    System.out.println("Sorry, you can't teach " + pokemonName + " anymore more moves.");
                    System.out.println("");
                    break;
                }
                else
                {
                    System.out.println(pokemonName + " has learned all of their moves");
                    System.out.println("");            
                }
            }
        }
        else
        {
            System.out.println(pokemonName + " has learned all of their moves");
            System.out.println("");
        }
        // End of first trainer


        // Second trainer
        System.out.println("Set up second Pokemon Trainer:");
        trainerName = readLine("Trainer, what is your name? ");
        PokemonTrainer pt2 = new PokemonTrainer(trainerName);
        System.out.println("Hello " + pt2 + "!");
        System.out.println("");
        System.out.println("Choose your first pokemon");
        pokemonName = readLine("Enter the name of your pokemon: ");
        System.out.println("You chose:");
        if(pokemonName.equalsIgnoreCase("Pikachu") || pokemonName.equalsIgnoreCase("Charmander") || pokemonName.equalsIgnoreCase("Bulbasaur") || pokemonName.equalsIgnoreCase("Squirtle"))
        {
            Pokemon p3 = new Pokemon(pokemonName, images.getPokemonImage(pokemonName));
            System.out.println(p3);
        }
        else
        {
            Pokemon p3 = new Pokemon(pokemonName);
            System.out.println(p3);
        }
        System.out.println("");
        newMove = readLine("Would you like to teach " + pokemonName +  " a new move? ");
        if(newMove.equalsIgnoreCase("yes"))
        {
            while(newMove.equalsIgnoreCase("yes") && numOfMoves3 < 4)
            {
                moveName = readLine("Enter the name of the move: ");
                moveDamage = readInt("How much damage does this move do? ");
                if(numOfMoves3 == 0)
                {
                    Move m9 = new Move(moveName, moveDamage);
                    p3.learnMove(m9);
                    System.out.println(pokemonName + " learned " + m9);
                }
                else if(numOfMoves3 == 1)
                {
                    Move m10 = new Move(moveName, moveDamage);
                    p3.learnMove(m10);
                    System.out.println(pokemonName + " learned " + m10);
                }
                else if(numOfMoves3 == 2)
                {
                    Move m11 = new Move(moveName, moveDamage);
                    p3.learnMove(m11);
                    System.out.println(pokemonName + " learned " + m11);
                }
                else if(numOfMoves3 == 3)
                {
                    Move m12 = new Move(moveName, moveDamage);
                    p3.learnMove(m12);
                    System.out.println(pokemonName + " learned " + m12);
                }
                numOfMoves3++;
                System.out.println("");
                newMove = readLine("Would you like to teach " + pokemonName +  " a new move? ");
                if(newMove.equalsIgnoreCase("yes") && numOfMoves3 < 4)
                {
                }
                else if(newMove.equalsIgnoreCase("yes") && numOfMoves3 >= 4)
                {
                    System.out.println("Sorry, you can't teach " + pokemonName + " anymore more moves.");
                    System.out.println("");
                    break;
                }
                else
                {
                    System.out.println(pokemonName + " has learned all of their moves");
                    System.out.println("");            
                }
            }
        }
        else
        {
            System.out.println(pokemonName + " has learned all of their moves");
            System.out.println("");
        }
        System.out.println("Choose your second pokemon");
        pokemonName = readLine("Enter the name of your pokemon: ");
        System.out.println("You chose:");
        if(pokemonName.equalsIgnoreCase("Pikachu") || pokemonName.equalsIgnoreCase("Charmander") || pokemonName.equalsIgnoreCase("Bulbasaur") || pokemonName.equalsIgnoreCase("Squirtle"))
        {
            Pokemon p4 = new Pokemon(pokemonName, images.getPokemonImage(pokemonName));
            System.out.println(p4);
        }
        else
        {
            Pokemon p4 = new Pokemon(pokemonName);
            System.out.println(p4);
        }
        System.out.println("");
        newMove = readLine("Would you like to teach " + pokemonName +  " a new move? ");
        if(newMove.equalsIgnoreCase("yes"))
        {
            while(newMove.equalsIgnoreCase("yes") && numOfMoves4 < 4)
            {

                moveName = readLine("Enter the name of the move: ");
                moveDamage = readInt("How much damage does this move do? ");
                if(numOfMoves4 == 0)
                {
                    Move m13 = new Move(moveName, moveDamage);
                    p4.learnMove(m13);
                    System.out.println(pokemonName + " learned " + m13);
                }
                else if(numOfMoves4 == 1)
                {
                    Move m14 = new Move(moveName, moveDamage);
                    p4.learnMove(m14);
                    System.out.println(pokemonName + " learned " + m14);
                }
                else if(numOfMoves4 == 2)
                {
                    Move m15 = new Move(moveName, moveDamage);
                    p4.learnMove(m15);
                    System.out.println(pokemonName + " learned " + m15);
                }
                else if(numOfMoves4 == 3)
                {
                    Move m16 = new Move(moveName, moveDamage);
                    p4.learnMove(m16);
                    System.out.println(pokemonName + " learned " + m16);
                }
                numOfMoves4++;
                System.out.println("");
                newMove = readLine("Would you like to teach " + pokemonName +  " a new move? ");
                if(newMove.equalsIgnoreCase("yes") && numOfMoves4 < 4)
                {
                }
                else if(newMove.equalsIgnoreCase("yes") && numOfMoves4 >= 4)
                {
                    System.out.println("Sorry, you can't teach " + pokemonName + " anymore more moves.");
                    System.out.println("");
                    break;
                }
                else
                {
                    System.out.println(pokemonName + " has learned all of their moves");
                    System.out.println("");            
                }
            }
        }
        else
        {
            System.out.println(pokemonName + " has learned all of their moves");
            System.out.println("");
        }
        // End of second trainer
        //Battle
        System.out.println("You have finished setting up the Pokemon and their trainers. \nGet ready for battle!");
        System.out.println("");
        System.out.println(pt1 + " brings out " + p1 + " to battle!");
        System.out.println(pt2 + " brings out " + p2 + " to battle!");
        while((p1.getHealth() + p2.getHealth()) == 200 || (p3.getHealth() + p4.getHealth()) == 200)
        {
            if(p1.getHealth() <= 100)
            {
                System.out.println(p1.getMoves());
                String p1Attack = readLine(pt1 + ", what attack do you want " + p1 + " to use? ");

            }
        }
    }
}

以下是我尝试运行此代码时显示的错误日志:

代码语言:javascript
复制
PokemonSimulation.java:50: error: cannot find symbol
                    p1.learnMove(m1);
                    ^
  symbol:   variable p1
  location: class PokemonSimulation
PokemonSimulation.java:56: error: cannot find symbol
                    p1.learnMove(m2);
                    ^
  symbol:   variable p1
  location: class PokemonSimulation
PokemonSimulation.java:62: error: cannot find symbol
                    p1.learnMove(m3);
                    ^
  symbol:   variable p1
  location: class PokemonSimulation
PokemonSimulation.java:68: error: cannot find symbol
                    p1.learnMove(m4);
                    ^
  symbol:   variable p1
  location: class PokemonSimulation
PokemonSimulation.java:120: error: cannot find symbol
                    p2.learnMove(m5);
                    ^
  symbol:   variable p2
  location: class PokemonSimulation
PokemonSimulation.java:126: error: cannot find symbol
                    p2.learnMove(m6);
                    ^
  symbol:   variable p2
  location: class PokemonSimulation
PokemonSimulation.java:132: error: cannot find symbol
                    p2.learnMove(m7);
                    ^
  symbol:   variable p2
  location: class PokemonSimulation
PokemonSimulation.java:138: error: cannot find symbol
                    p2.learnMove(m8);
                    ^
  symbol:   variable p2
  location: class PokemonSimulation
PokemonSimulation.java:198: error: cannot find symbol
                    p3.learnMove(m9);
                    ^
  symbol:   variable p3
  location: class PokemonSimulation
PokemonSimulation.java:204: error: cannot find symbol
                    p3.learnMove(m10);
                    ^
  symbol:   variable p3
  location: class PokemonSimulation
PokemonSimulation.java:210: error: cannot find symbol
                    p3.learnMove(m11);
                    ^
  symbol:   variable p3
  location: class PokemonSimulation
PokemonSimulation.java:216: error: cannot find symbol
                    p3.learnMove(m12);
                    ^
  symbol:   variable p3
  location: class PokemonSimulation
PokemonSimulation.java:268: error: cannot find symbol
                    p4.learnMove(m13);
                    ^
  symbol:   variable p4
  location: class PokemonSimulation
PokemonSimulation.java:274: error: cannot find symbol
                    p4.learnMove(m14);
                    ^
  symbol:   variable p4
  location: class PokemonSimulation
PokemonSimulation.java:280: error: cannot find symbol
                    p4.learnMove(m15);
                    ^
  symbol:   variable p4
  location: class PokemonSimulation
PokemonSimulation.java:286: error: cannot find symbol
                    p4.learnMove(m16);
                    ^
  symbol:   variable p4
  location: class PokemonSimulation
PokemonSimulation.java:317: error: cannot find symbol
        System.out.println(pt1 + " brings out " + p1 + " to battle!");
                                                  ^
  symbol:   variable p1
  location: class PokemonSimulation
PokemonSimulation.java:318: error: cannot find symbol
        System.out.println(pt2 + " brings out " + p2 + " to battle!");
                                                  ^
  symbol:   variable p2
  location: class PokemonSimulation
PokemonSimulation.java:319: error: cannot find symbol
        while((p1.getHealth() + p2.getHealth()) == 200 || (p3.getHealth() + p4.getHealth()) == 200)
               ^
  symbol:   variable p1
  location: class PokemonSimulation
PokemonSimulation.java:319: error: cannot find symbol
        while((p1.getHealth() + p2.getHealth()) == 200 || (p3.getHealth() + p4.getHealth()) == 200)
                                ^
  symbol:   variable p2
  location: class PokemonSimulation
PokemonSimulation.java:319: error: cannot find symbol
        while((p1.getHealth() + p2.getHealth()) == 200 || (p3.getHealth() + p4.getHealth()) == 200)
                                                           ^
  symbol:   variable p3
  location: class PokemonSimulation
PokemonSimulation.java:319: error: cannot find symbol
        while((p1.getHealth() + p2.getHealth()) == 200 || (p3.getHealth() + p4.getHealth()) == 200)
                                                                            ^
  symbol:   variable p4
  location: class PokemonSimulation
PokemonSimulation.java:321: error: cannot find symbol
            if(p1.getHealth() <= 100)
               ^
  symbol:   variable p1
  location: class PokemonSimulation
PokemonSimulation.java:323: error: cannot find symbol
                System.out.println(p1.getMoves());
                                   ^
  symbol:   variable p1
  location: class PokemonSimulation
PokemonSimulation.java:324: error: cannot find symbol
                String p1Attack = readLine(pt1 + ", what attack do you want " + p1 + " to use? ");
                                                                                ^
  symbol:   variable p1
  location: class PokemonSimulation
25 errors
EN

回答 1

Stack Overflow用户

发布于 2020-01-20 10:23:29

您的Pokemon p1 = ...受限于其周围if(...){}的作用域。在更高的作用域中声明它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59816219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档