You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java文本游戏初始化报错:找不到initGame()方法,求数组填充方案

问题解决:cannot find symbol: method initGame()

编译错误信息

Game4.java:50: error: cannot find symbol
        initGame();
        ^
  symbol:   method initGame()
  location: class Game
1 error

你的代码

class Game {
 
    // Declare and initialize global variables
    private static final int num_locations = 5;
    private static final int num_characters = 5;
    private static final int num_items = 12;
 
    private Location[] locations;
    private Character[] characters;
    private Item[] items;
 
    private int currentLocation;
 
    // Default constructor
    public Game() {
         
        // Create the array of locations
        locations = new Location[num_locations];//NUM_LOCATIONS];

        // Create the array of characters
        characters = new Character[num_characters];//NUM_CHARACTERS];
         
        // Create the array of items
        items = new Item[num_items];//NUM_ITEMS];
         
        // Initialize the game by populating the arrays
        // with objects
        initGame(); // find a new way to initialize the game by populating the game
  
        // Set the current location to the starting location
        currentLocation = 0;
         
    }
}

解决思路

错误原因很直接:你在构造函数里调用了initGame()方法,但这个方法在Game类中完全没有定义,编译器找不到对应的方法符号,所以报错。下面提供两种可行的初始化方案:

方案1:补充定义initGame()方法

在Game类中添加initGame()方法,把填充数组的逻辑写在这个方法里,示例如下:

class Game {
    // ... 原有的变量声明部分 ...

    // Default constructor
    public Game() {
        // ... 原有的数组创建代码 ...
        initGame();
        currentLocation = 0;
    }

    // 新增initGame方法,实现数组填充逻辑
    private void initGame() {
        // 填充locations数组示例
        locations[0] = new Location("入口", "这是游戏的起始地点");
        locations[1] = new Location("森林", "一片幽暗的森林");
        // ... 按需完成其他Location对象的初始化

        // 填充characters数组示例
        characters[0] = new Character("守卫", "守护入口的士兵");
        // ... 按需完成其他Character对象的初始化

        // 填充items数组示例
        items[0] = new Item("钥匙", "打开宝箱的钥匙");
        // ... 按需完成其他Item对象的初始化
    }
}

方案2:直接在构造函数中完成初始化

如果不需要单独的初始化方法,直接把数组填充逻辑写到构造函数里,替换掉initGame()的调用即可:

class Game {
    // ... 原有的变量声明部分 ...

    // Default constructor
    public Game() {
        // 创建数组
        locations = new Location[num_locations];
        characters = new Character[num_characters];
        items = new Item[num_items];

        // 直接填充locations数组
        locations[0] = new Location("入口", "这是游戏的起始地点");
        locations[1] = new Location("森林", "一片幽暗的森林");
        // ... 按需完成其他Location对象的初始化

        // 直接填充characters数组
        characters[0] = new Character("守卫", "守护入口的士兵");
        // ... 按需完成其他Character对象的初始化

        // 直接填充items数组
        items[0] = new Item("钥匙", "打开宝箱的钥匙");
        // ... 按需完成其他Item对象的初始化

        currentLocation = 0;
    }
}

内容的提问来源于stack exchange,提问作者JAVAnew123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 09:45:41