gpt4 book ai didi

linux - 尝试为我的 Java 应用程序创建一个包

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:58:18 27 4
gpt4 key购买 nike

我正在用 Java 构建一个简单的 RPG 游戏引擎。此时一切正常,而我所有的类都在一个目录中。基本上,我知道我最终会得到一堆文件,并希望将它们组织成一个包结构。我按照 http://www.jarticles.com/package/package_eng.html 的指示进行操作但似乎无法让魔法发生。发布的两个类是最不相关的,我想如果我能让它们工作,那么剩下的就不应该是戏剧了。作为记录,我在 Leeenux 中使用 openJDK(Ubuntu 上网本 Remix 的混音)

头等舱

package adventure.engine;

import java.util.*;

public class Inventory
{
ArrayList itemList = new ArrayList();

public Inventory()
{

}

public void addItem()
{

}

public void removeItem()
{

}
}

第二个:

package adventure.engine;



import adventure.engine.*;



public class PlayerCharacter

{

private String name = "Player";

private String race;

private String plrClass;

private int level;

private int xp;

private int levelXp;

private Inventory inventory = new Inventory();



//---------

//Abilities

//---------



private static final String[] abilitiesList = {"Strength",

"Dexterity",

"Constitution",

"Intelligence",

"Wisdom",

"Charisma"};



private int[] abilitiesValues = new int[abilitiesList.length];



//------

//Skills

//------

private static final String[] skillsList = {"Acrobatics" , "Insight",

"Arcana" , "Intimidate",

"Athletics" , "Nature",

"Bluff" , "Perception",

"Diplomacy" , "Religion",

"Dungeoneering" , "Stealth",

"Endurance" , "Streetwise",

"Heal" , "Thievery",

"History"};



private int[] skillsValues = new int[skillsList.length];



//***********

//Constructor

//***********

public PlayerCharacter()

{

level = 1;

xp = 0;

levelXp = 1000;



setAbility("Strength", 8);

setAbility("Dexterity", 10);

setAbility("Constitution", 10);

setAbility("Intelligence", 10);

setAbility("Wisdom", 10);

setAbility("Charisma", 10);

} //public PlayerSheet()





//*************

//Class Methods

//*************

public void addXp(int val)

{

xp += val;



if (xp >= levelXp)

{

level++;

xp -= levelXp;

//levelXp += ;

}

} //public void addXp(int val)





public void updateSkills()

{



}





//Mutators

public void setName(String n)

{

name = n;

}





public void setLevel(int l)

{

level = l;

}



public void setRace(String r)

{

race = r;

}



public void setXP(int x)

{

xp = x;

}





public void setClass(String c)

{

plrClass = c;

}



//set ability value by name

public void setAbility(String a, int val)

{

for(int i = 0; i < abilitiesList.length; i++)

{

if(abilitiesList[i].compareTo(a) == 0)

{

abilitiesValues[i] = val;

}

}

}



//set ability by index

public void setAbility(int index, int val)

{

abilitiesValues[index] = val;

}



//set skill by name

public void setSkill(String name, int val)

{

for(int i = 0; i < skillsList.length; i++)

{

if(skillsList[i].compareTo(name) == 0)

{

skillsValues[i] = val;

}

}

}



//set skill by index

public void setSkill(int index, int val)

{

skillsValues[index] = val;

}



//Accessors

public static String[] getAbilityList()

{

return abilitiesList;

}



public static String[] getSkillsList()

{

return skillsList;

}



//retrieve an ability value by name

public int getAbility(String a)

{

int val = 0;



for(int i = 0; i < abilitiesList.length; i++)

{

if(abilitiesList[i].compareTo(a) == 0)

{

val = abilitiesValues[i];

break;

}

}



return val;

}



//retrieve an ability value by index number

public int getAbility(int i)

{

return abilitiesValues[i];

}



public int getSkill(String s)

{

int val = 0;



for(int i = 0; i < skillsList.length; i++)

{

if(skillsList[i].compareTo(s) == 0)

{

val = skillsValues[i];

break;

}

}



return val;

}



public int getSkill(int i)

{

return skillsValues[i];

}



public String getName()

{

return name;

}



public String getRace()

{

return race;

}



public String getPlrClass()

{

return plrClass;

}



public int getLevel()

{

return level;

}



public int getXP()

{

return xp;

}



public int getLevelXP()

{

return levelXp;

}



} //public class PlayerCharacter

类位于/home/user/Java/adventure/engine

echo $classpath 的输出是/home/james/Java:/.:

当我尝试编译第二个类时出现以下错误:

PlayerCharacter.java:18: cannot find symbol
symbol : class Inventory
location: class adventure.engine.PlayerCharacter
private Inventory inventory = new Inventory();
^
PlayerCharacter.java:18: cannot find symbol
symbol : class Inventory
location: class adventure.engine.PlayerCharacter
private Inventory inventory = new Inventory();

如有任何反馈,我们将不胜感激。如何解决?

最佳答案

两件事。

1) You might not have compiled Inventory
2) PlayerCharacter and Inventory are in same package. So there is no need to import.

你应该将它们编译为

javac adventure/engine/Inventory.java

javac adventure/engine/PlayerCharacter.java

关于linux - 尝试为我的 Java 应用程序创建一个包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12758122/

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