开发者

Role Playing Game program: calling input variables from another class

I have been searching for a solution for this for hours and I'm stumped. I'm very new to Java, but I am currently programming a very easy role playing game engine. In this game, the user is to type in the level of his stats. My question is: how can I make a class where this information is stored and easily referenced when the level of the stat is called for?

To be a little more specific: I want to create a variable where the user puts in the level for his skill (using the Scanner), and then I want to let the rest of the java program access the value and use it throughout the game.

I would prefer keeping the stats in anoth开发者_开发问答er class than where the stats will be used.

Thanks in advance for the answers!


How about

class Stats {
    private int skillLevel;
    // ... other stats
    public void setSkill(int level) { skillLevel = level }
    public int getSkill() { return skillLevel; }
}

class Player {
    private Stats stats;
    // ...
    public Stats getStats() { return stats; }
}

You cant then get skill for a player:

Player p;
// ...
p.getStats().getSkill();

Was it that simple or did I fail to notice something?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜