我开始研究基本的RPG游戏,我有一点问题。我有一个Character类,它是我的英雄和他的数据。我正在使用合成来创造一个角色。我遇到的问题是在CharacterCreator
方法的createCharacter
中,hero
出现了问题。为什么,我该如何解决这个问题?这是代码:
public class Game {
public static void main(String[] args) throws IOException {
Character h = CharacterCreator.createCharacter();
try {
h.displayCharacter();
} catch (Exception e) {
System.out.println("Wrong");
}
}
}
public class CharacterCreator {
public static Character createCharacter() {
System.out.println("Choose a character: ");
System.out.println("1. Fighter");
System.out.println("2. Rogue");
System.out.println("3. Mage");
System.out.println("4. Cleric");
Scanner sc = new Scanner(System.in);
int scan = sc.nextInt();
String chosenClass = getCharacterClass(scan);
System.out.println("Choose Name:");
Scanner nameIn = new Scanner(System.in);
String name = nameIn.next();
Character hero = null;
switch (chosenClass) {
case "Fighter":
Fighter hero = new Fighter(name);
break;
case "Rogue":
Rogue hero = new Rogue(name);
break;
case "Mage":
Mage hero = new Mage(name);
break;
case "Cleric":
Cleric hero = new Cleric(name);
break;
}
return hero;
}
public static String getCharacterClass(int scan) {
String classIn;
switch (scan) {
case 1:
classIn = "Fighter";
break;
case 2:
classIn = "Rogue";
break;
case 3:
classIn = "Mage";
break;
case 4:
classIn = "Cleric";
break;
default:
System.out.println("Enter again");
classIn = "def";
}
return classIn;
}
}
public class Character {
private String name;
private String characterClass;
private int level;
private int hp;
private int currentHp;
private int armorClass;
private long xp;
/* private int BAB; /*Base attack bonus */
private int strength;
private int constitution;
private int dexterity;
private int intelligence;
private int wisdom;
private int charisma;
protected Character(String name) {
setName(name);
characterClass = "Class";
setLevel(1);
setStrength(10);
setConstitution(14);
setDexterity(14);
setIntelligence(10);
setWisdom(10);
setCharisma(10);
setHp(0 + getModifier(getConstitution()));
setCurrentHp(getHp());
setArmorClass(10 + getModifier(getDexterity()));
setXp(0);
}
void displayCharacter() throws IOException {
System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
System.out.println("Name: " + getName());
System.out.println("Class: " + getCharacterClass());
System.out.println("Level: " + getLevel());
System.out.println("HP: " + getHp());
System.out.println("Armor Class: " + getArmorClass());
System.out.println("***************");
System.out.println("Attributes: ");
System.out.println("Strength: " + getStrength());
System.out.println("Constitution: " + getConstitution());
System.out.println("Dexterity: " + getDexterity());
System.out.println("Intelligence: " + getIntelligence());
System.out.println("Wisdom: " + getWisdom());
System.out.println("Charisma: " + getCharisma());
System.out.println("***************");
System.out.println("XP: " + getXp());
}
public int getModifier(int number) {
int mod = (int) ((number - 10) / 2);
return mod;
}
public String getName() {
return name;
}
public String getCharacterClass() {
return characterClass;
}
public int getLevel() {
return level;
}
public int getHp() {
return hp;
}
public int getCurrentHp() {
return currentHp;
}
public int getArmorClass() {
return armorClass;
}
public int getStrength() {
return strength;
}
public int getConstitution() {
return constitution;
}
public int getDexterity() {
return dexterity;
}
public int getIntelligence() {
return intelligence;
}
public int getWisdom() {
return wisdom;
}
public int getCharisma() {
return charisma;
}
public long getXp() {
return xp;
}
protected void setName(String Name) {
name = Name;
}
protected void setCharacterClass(String characterClass) {
this.characterClass = characterClass;
}
protected void setLevel(int lvl) {
level = lvl;
}
protected void setHp(int hitPoints) {
hp = hitPoints;
}
protected void setCurrentHp(int curHp) {
currentHp = curHp;
}
protected void setArmorClass(int ac) {
armorClass = ac;
}
protected void setStrength(int str) {
strength = str;
}
protected void setConstitution(int con) {
constitution = con;
}
protected void setDexterity(int dex) {
dexterity = dex;
}
protected void setIntelligence(int intel) {
intelligence = intel;
}
protected void setWisdom(int wis) {
wisdom = wis;
}
protected void setCharisma(int cha) {
charisma = cha;
}
protected void setXp(int XP) {
xp = XP;
}
}
class Fighter extends CharacterClass {
Fighter(String name) {
Character hero = new Character(name);
hero.setName(name);
hero.setCharacterClass("Fighter");
hero.setLevel(1);
hero.setStrength(14);
hero.setConstitution(16);
hero.setDexterity(14);
hero.setIntelligence(10);
hero.setWisdom(10);
hero.setCharisma(10);
hero.setHp(10 + hero.getModifier(getConstitution()));
hero.setCurrentHp(hero.getHp());
hero.setArmorClass(10 + hero.getModifier(getDexterity()));
}
}
答案 0 :(得分:1)
我可以在您的程序中看到潜在的编译器错误。这是罪魁祸首:
switch (chosenClass){
case "Fighter":
Fighter hero = new Fighter(name);
break;
case "Rogue":
Rogue hero = new Rogue(name);
break;
case "Mage":
Mage hero = new Mage(name);
break;
case "Cleric":
Cleric hero = new Cleric(name);
break;
}
在切换案例之外声明hero
变量一次。
编辑:
从createCharacter
方法检查此修改过的代码段。
Character hero = null;
switch (chosenClass){
case "Fighter":
hero = new Fighter(name);
break;
case "Rogue":
hero = new Rogue(name);
break;
case "Mage":
hero = new Mage(name);
break;
case "Cleric":
hero = new Cleric(name);
break;
}
这是你应该主要做的事情。执行此操作后,您会看到更多错误。这是因为,您已声明createCharacter
方法返回Character的实例,但Mage,Cleric, Fighter,Rogue
是CharacterClass
的所有子类。我在代码中没有看到名为CharacterClass
的类。也许这是Character
- 你所拥有的课程。因此,我建议将4个以上的子类更改为从字符扩展而不是CharacterClass
类,我在代码中看不到它。
最后,在需要初始化时,从基类构造函数中调用超类构造函数。
答案 1 :(得分:0)
您正在切换案例中创建新的英雄实例,并将这些实例分配给本地变量。您在输入switch语句之前创建的hero变量将始终保持为null。您需要将新英雄分配给此变量。
Character hero = null;
switch (chosenClass){
case "Fighter":
hero = new Fighter(name);
break;
case "Rogue":
hero = new Rogue(name);
break;
case "Mage":
hero = new Mage(name);
break;
case "Cleric":
hero = new Cleric(name);
break;
}
return hero;