1

我正在创建一个选择你自己的冒险“游戏”作为小组项目的一部分。我的想法是创建一个 StoryRoom 类型的对象,它将存储诸如对该 StoryRoom 的描述、其他 StoryRoom 连接到它的信息等信息。我还希望有一个类来创建一个冒险,这将是 StoryRoom 的数组. 这个想法是主程序将在 StoryRoom 中循环,显示文本并在做出决定时返回一个值,该值将作为下一个 StoryRoom 的参考,直到玩家到达结束场景。

我遇到的问题是我无法通过 AdventureTeller 类访问在 AdventureBuilder 类中创建的 StoryRooms。AdventureTeller 是游戏的驱动程序类。Eclipse 抛出 entryway 无法解析为变量的错误。我怎样才能解决这个问题?

public class StoryRoom {
    public String[] description = null;
    public int[] exits;


    public StoryRoom(String[] builddescriptions, int[] buildexits){
        description = new String[builddescriptions.length];
        exits = new int[buildexits.length];
        for (int i = 0; i< builddescriptions.length; i++){
            description[i] = builddescriptions[i];
        }
        for (int i = 0; i< buildexits.length; i++){
            exits[i] = buildexits[i];
        }

    }

    public String GetDescription(int reference){
        return description[reference];
    }

    public int GetExit(int reference){
        return exits[reference];
    }

}


public class AdventureTeller {


    public static void main(String[] args) {
        AdventureBuilder.main();
        for (int i = 0; i < entryway.description.length; i++)
        {
            System.out.println(entryway.GetDescription(i));
        }

    }

}




public class AdventureBuilder {

    public static void main() {
        // StoryRoom[] book = new StoryRoom[20];
        String[] description = null;
        description = new String[3];
        int[] exits;
        exits = new int[3];

        description[0] = "This is a strange room with 2 doors, one on the left and one on the right.";
        description[1] = "Take the left door? (enter 1)";
        description[2] = "Take the right door? (enter 2)";
        exits[0] = 1; // current room number
        exits[1] = 2;
        exits[2] = 3;
        StoryRoom entryway = new StoryRoom(description, exits);
    }

}
4

3 回答 3

3

在您发布的代码中,entryway它不是实例变量,而是方法变量——它的范围仅限于您的AdventureBuilder.main()方法。如果您希望它在类之外和该方法之外可访问,请将其设置为实例变量(或返回它,然后调用方法可以使用它)。

于 2013-04-22T03:23:57.537 回答
2

仅更改 AdventureBuilder 是不够的。您还需要更改 AdventureTeller。以下两个类的新版本(针对 Java 1.6 进行了测试):

public class AdventureBuilder
{
    private StoryRoom entryway;

    // Avoid naming a method 'main' unless it's the main method of your main class
    // Try something like 'initAdventureBuilder()' instead
    public void main()
    {
        // StoryRoom[] book = new StoryRoom[20];
        String[] description = null;
        description = new String[3];
        int[] exits;
        exits = new int[3];

        description[0] = "This is a strange room with 2 doors, one on the left and one on the right.";
        description[1] = "Take the left door? (enter 1)";
        description[2] = "Take the right door? (enter 2)";

        exits[0] = 1; // current room number
        exits[1] = 2;
        exits[2] = 3;

        entryway = new StoryRoom(description, exits);
    }

    // A way for AdventureBuilder to be questioned about its entryway field

    public StoryRoom getStoryRoom()
    {
        return entryway;
    }
}

public class AdventureTeller
{
    public static void main(String[] args)
    {
        // Creating a copy of AdventureBuilder that AdventureTeller knows about
        AdventureBuilder ab = new AdventureBuilder();
        StoryRoom entryway;

        // Asking AdventureBuilder to run itself 
        ab.main();

        // Asking AdventureBuilder for a reference to the entryway which it created
        // when you asked AdventureBuilder to run itself

        entryway = ab.getStoryRoom();

        for (int i = 0; i < entryway.description.length; i++)
        {
            System.out.println(entryway.GetDescription(i));
        }
    }
}

除了启动程序的方法之外,您可能希望避免将main其用作任何方法的方法名称。尝试使用类似initAdventureBuilder()或类似的描述性方法名称,以避免混淆。当我第一次查看您的代码时,由于您的命名约定,我认为 AdventureBuilder 是主类,但实际上并非如此。

为了让您知道发生了什么,当您尝试编译时,编译器抱怨不知道 AdventureTeller 中的 entryway 字段指的是什么。即使您已经注释掉那行代码只是为了让它编译,AdventureTeller 仍然不知道 AdventureBuilder 里面有什么字段,除非您明确告诉 AdventureTeller 里面有什么。更糟糕的是,您从未在 AdventureTeller 中创建 AdventureBuilder 的副本。

于 2013-04-22T03:36:59.200 回答
1

只需根据 Catherine 的建议修复您的代码,它应该是这样的。

...

public class AdventureBuilder {

    StoryRoom entryway;

    public static void main() {
        // StoryRoom[] book = new StoryRoom[20];
        String[] description = null;
        description = new String[3];
        int[] exits;
        exits = new int[3];

        description[0] = "This is a strange room with 2 doors, one on the left and one on the right.";
        description[1] = "Take the left door? (enter 1)";
        description[2] = "Take the right door? (enter 2)";
        exits[0] = 1; // current room number
        exits[1] = 2;
        exits[2] = 3;
        entryway = new StoryRoom(description, exits);
    }
}
于 2013-04-22T03:28:49.440 回答