1

グループ プロジェクトの一環として、自分で選ぶアドベンチャー「ゲーム」を作成しています。私の考えは、その StoryRoom の説明、それに接続している他の StoryRoom などの情報を格納するタイプの StoryRoom のオブジェクトを作成することです。また、StoryRoom の配列になる冒険を作成するためのクラスも必要です。 . アイデアは、プレイヤーが最終シナリオに到達するまで、メイン プログラムが StoryRooms を循環し、テキストを表示し、次の 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 に何が含まれているかを明示的に伝えない限り、AdventureTeller は AdventureBuilder 内にどのフィールドがあったかを知ることができません。さらに悪いことに、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 に答える