语法错误,插入“VariableDeclarators”来完成LocalVariableDeclaration - Java ChooseYourAdventure游戏

时间:2017-11-21 03:38:56

标签: java

我需要一些帮助,我正在用Java制作一个“冒险”游戏。我有四个班级,其中三个写在下面 - 最后一个班级现在无关紧要。在Storyline中,keySet()似乎没有注册我有一个Storyline对象,其中包含一个名为“key”的键。我也无法确定Storylines课程的范围,如ChooseYourAdventure所示。我得到了

  

“语法错误,插入”VariableDeclarators“以完成LocalVariableDeclaration”和“Storylines.s1无法解析为类型”

在主函数中调用Storylines.s1时出现

错误。此外,当我在main方法之外的numberOfStories类中调用Storyline构造函数时,静态ChooseYourAdventure变量似乎没有递增。任何解决任何这些问题的帮助都非常感谢!

ChooseYourAdventure

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;
    }
}

1 个答案:

答案 0 :(得分:0)

在您的情况下,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();    
        }
      }

您必须在主方法中将值初始化为Storylines.s1的相同方法。不会从Story类中初始化的对象中获取值。