我需要一些帮助来制作我正在用Java制作的"选择你的冒险"游戏。我有四节课,其中三节写在下面——最后一堂暂时无关紧要。 在Storyline
中,keySet()
似乎没有注册我有一个Storyline
对象,带有一个名为"key"的键。我也很难弄清楚我的Storylines
类的范围,如ChooseYourAdventure
所示。我得到一个
">语法错误,插入"变量声明器"以完成 LocalVariableDeclaration"和"无法解析为 Storylines.s1 的类型"
当我在主函数中调用Storylines.s1
时出错。此外,当我在 main 方法之外的 ChooseYourAdventure
类中调用 Storyline
构造函数时,静态 numberOfStories
变量似乎没有递增。任何帮助解决这些问题,非常感谢!
选择你的冒险
public class ChooseYourAdventure {
private static ChooseYourAdventure cyaGame;
private static Character player;
public ChooseYourAdventure()
{
ChooseYourAdventure cyaGame = new ChooseYourAdventure();
Character player = new Character();
}
public static void main(String[] args)
{
// while(player.checkIsDead() == false)
// {
// cyaGame.nextStoryLine();
// }
final Storyline s = new Storyline(0, 0, 0, "heyo", "key");
Storylines.s1;
System.out.println("key word: " + s.getKeyWord());
Storyline.printStories();
}
}
故事 情节
public final class Storylines {
private static final String text1 = "hey boi";
public static final Storyline s1 = new Storyline(0, 0, 0, text1, "key");
}
故事 情节
import java.util.HashMap;
public class Storyline {
public static int numberOfStories = 0;
public static HashMap<String, Storyline> stories = new HashMap<String, Storyline>();
private int ageChange;
private int happinessChange;
private int meaningChange;
private String text = "";
private String keyWord = "";
public Storyline(int a, int h, int m, String t, String k)
{
numberOfStories++;
stories.put(this.keyWord, this);
this.ageChange = a;
this.happinessChange = h;
this.meaningChange = m;
this.text = t;
this.keyWord = k;
}
public static void printStories()
{
System.out.println("number of Stories: " + numberOfStories);
System.out.println("Keys:");
System.out.println("stories.keySet() = " + stories.keySet());
for(String k : stories.keySet())
{
System.out.println(k); // k is not showing up in keySet()?
}
System.out.println("Text:");
for(Storyline s : stories.values())
{
System.out.println(s.getText());
}
}
public int getAgeChange()
{
return this.ageChange;
}
public int getHappinessChange()
{
return this.happinessChange;
}
public int getMeaningChange()
{
return this.meaningChange;
}
public String getText()
{
return this.text;
}
public String getKeyWord()
{
return this.keyWord;
}
}
在您的情况下,StoryLines.s1 期望一些值被初始化,这就是编译器给您错误的原因。比如说,
public class StaticClass {
static int value ;
public void show(){
System.out.println("Value:: "+value);
}
public static void main(String[] args) {
StaticClass.value = 9; //intialize value
StaticClass sc = new StaticClass();
sc.show();
}
}
同样,您必须在main方法中将值初始化为Storylines.s1。它不会从故事类中初始化的对象中获取值。