gpt4 book ai didi

Java - 集合

转载 作者:太空宇宙 更新时间:2023-11-04 13:15:24 29 4
gpt4 key购买 nike

我正在开发我的第一个 Java 项目,我有一个问题。问题应该很简单(虽然代码不是那么短,但没有理由被吓倒:))。我创建了一个基本的角色扮演游戏,并且有一个定义每个角色的抽象类“Character”。在它的子类别中,你可以找到法师,他有一本法术书( map )。 Spellbook 类提供了像 addToSpellbook 这样的方法,效果很好。此外,我有一个 Inventory 类,它具有 addToInventory 方法,该方法与 addToSpellbook 完全相同。

我的问题如下 - 为什么我可以在 main 方法中使用 addToSpellbook 而不能使用 AddToInventory?

我猜原因是 Map 没有 AddToInventory,所以我应该重写 put,但是,我如何使用 addToSpellbook ?

public class Game {
public static void main(String[] args) throws IOException {

CharacterCreator heroCreator = new CharacterCreator();
CharacterCreator.showAllClasses();

Scanner sc = new Scanner(System.in);
int scan = sc.nextInt();
String chosenClass = CharacterCreator.getCharacterClass(scan);
Character hero = CharacterCreator.createCharacter(chosenClass);
try {
hero.displayCharacter();
}catch (Exception e){
System.out.println("Problem displaying character data");
}

hero.getInventory().addToInventory("Long sword");


CharacterCreator heroCreator2 = new CharacterCreator();
CharacterCreator.showAllClasses();

Scanner sc2 = new Scanner(System.in);
int scan2 = sc.nextInt();
String chosenClass2 = CharacterCreator.getCharacterClass(scan2);
Character hero2 = CharacterCreator.createCharacter(chosenClass2);
try {
hero2.displayCharacter();
}catch (Exception e){
System.out.println("Wrong input");
}

if(hero instanceof Mage) {
((Mage)hero).getSpellBook().addToSpellBook("Magic Missiles");
((Mage)hero).getSpellBook().addToSpellBook("Fireball");
((Mage)hero).getSpellBook().addToSpellBook("Mage Armor");

((Mage)hero).getSpellBook().showSpellBook();
((Mage)hero).getSpellBook().getSpellFromSpellbook("Fireball").castSpell(hero, hero2);
((Mage)hero).getSpellBook().getSpellFromSpellbook("Magic Missiles").castSpell(hero, hero2);
((Mage)hero).getSpellBook().getSpellFromSpellbook("Mage Armor").castSpell(hero, hero);

}


}

}


abstract public class Character {



private Equipment equipment;

private Map<String, Integer> inventory;



protected Character(String name){

equipment = new Equipment();

inventory = new HashMap<String, Integer>();

}

protected Character(String name, int lvl){


equipment = new Equipment();

inventory = new HashMap<String, Integer>();

}


}






public Equipment getEquipment() { return equipment; }
public Map getInventory() { return inventory; }


}

public class Inventory {

private Map<String,Integer> inventory;

Inventory() {

inventory = new HashMap<String, Integer>();
}

public void addToInventory(String item) {

boolean found = false;

try {
for (Iterator<Map.Entry<String, Integer>> iter = inventory.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry<String, Integer> newItem = iter.next();
if (newItem.getKey() == item) {
inventory.put(item, inventory.get(newItem) + 1);
break;
}
}
}catch (Exception e) {
System.out.println(item + " : adding failed");
}

if (!found) {
inventory.put(item,1);
}

}


public void showInventory() {

System.out.println("Show Inventory: ");

for (Map.Entry<String,Integer> entry: inventory.entrySet()) {
System.out.println( entry.getKey() + ", quantity: " + entry.getValue() );
}
System.out.println("");
}

}


public class Mage extends Character {


private SpellBook spellBook;


public Mage(String name) {

super(name);

SpellBook spellbook = new SpellBook();

}

protected Mage(String name, int lvl){

super(name, lvl);


spellBook = new SpellBook();


}


public SpellBook getSpellBook() { return spellBook; }


}



}


public class SpellBook {

private Map<String, Spell> spellBook;

SpellBook() {

spellBook = new HashMap<String, Spell>();
}

public Map getSpellBook() { return spellBook; }


public void addToSpellBook(String spellName) {

Spell newSpell = null;

try {

if (DamageSpell.getSpell(spellName) != null) {
newSpell = DamageSpell.getSpell(spellName);
} else if (ChangeStatSpell.getSpell(spellName) != null) {
newSpell = ChangeStatSpell.getSpell(spellName);
}

System.out.println(newSpell.getSpellName() + " has been added to the spellbook");
spellBook.put(newSpell.getSpellName(), newSpell);

} catch (Exception e){
System.out.println("Adding " + spellName +"to spellbook has failed");
}

}


public void showSpellBook() {

System.out.println("Show spellbook: ");

for (Iterator<String> iter = spellBook.keySet().iterator(); iter.hasNext(); ) {
String spell = iter.next();
System.out.println(spell);
}
System.out.println("");
}

public Spell getSpellFromSpellbook(String spellName) {

Spell spl = null;

//Spell splGet = spellBook.get(spellName); /* straight forward implementation*/
// System.out.println("The spell " + splGet.getSpellName() + " has been retrived from the spellbook by using get method");

try {
for (Iterator<Map.Entry<String, Spell>> iter = spellBook.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry<String, Spell> spell = iter.next();
if (spell.getKey() == spellName) {
spl = spell.getValue();
}
}
}catch (Exception e) {
System.out.println(spellName + " : no such spell in spellbook");
}
return spl;

}

}

最佳答案

getInventory() 返回一个 Map,而 Map 没有 addToInventory() 方法。getInventory() 应添加一个 Inventory 实例。

关于Java - 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33593554/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com