- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有三个名为 Pokemon.java、Move.java 和 PokemonTrainer.java 的类,它们包含创建和修改 Pokemon、它们的 Action 和训练器的方法。我已经创建了所有必需的方法,但在调用我在 PokemonSimulation.java 类中创建的 Pokemon 对象时遇到问题,程序总是无法编译,并出现错误“java:56: error: 找不到符号” ”。我该如何解决这个问题?
这是 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 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 的代码:
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 的代码:
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 的代码:
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? ");
}
}
}
}
这是我尝试运行此代码时显示的错误日志:
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
最佳答案
您的 Pokemon p1 = ...
仅限于其周围 if(...){}
的范围。在更高的范围内声明它。
关于java - 无法调用 Pokemon 类中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59816219/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!