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

Java海龟项目load命令触发无限循环问题修复咨询

解决Java海龟项目中load命令导致的无限循环问题

问题代码

private void executeCommandsFromFile(String filename) {
    try {
        BufferedReader reader = new BufferedReader(new FileReader(filename));
        String line = reader.readLine();
        while (line != null) {
            processCommand(line);
            line = reader.readLine();
        }
        System.out.println("Commands executed from file: " + filename);
    } catch (FileNotFoundException e) {
        System.out.println("File not found: " + filename);
    } catch (IOException e) {
        System.out.println("Error reading file: " + filename);
    }
}

public void processCommand(String command)              
{
    if (command.equals("save")) {
        saveCommandsToFile("commands.txt");
    }
    else if (command.equals("load")) {
        executeCommandsFromFile("commands.txt");
    }
}

问题原因

当commands.txt文件中包含load命令时,executeCommandsFromFile读取到该命令后会调用processCommand,进而再次触发executeCommandsFromFile("commands.txt"),形成无限递归循环;如果文件中同时包含save命令,每次递归都会执行保存操作,导致保存命令被反复触发。此外,原代码未正确关闭文件资源,存在泄漏风险。

修复方案

方案1:跳过加载文件中的load/save命令

直接在读取文件时过滤掉会触发递归或循环的命令,从根源避免问题:

private void executeCommandsFromFile(String filename) {
    // 使用try-with-resources自动关闭文件资源,避免泄漏
    try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
        String line = reader.readLine();
        while (line != null) {
            // 跳过load和save命令,防止递归循环
            if (!line.equals("load") && !line.equals("save")) {
                processCommand(line);
            }
            line = reader.readLine();
        }
        System.out.println("Commands executed from file: " + filename);
    } catch (FileNotFoundException e) {
        System.out.println("File not found: " + filename);
    } catch (IOException e) {
        System.out.println("Error reading file: " + filename);
    }
}

方案2:添加递归深度限制(支持加载其他文件场景)

如果需要允许加载文件中包含load命令(比如加载其他不同的命令文件),可以通过递归深度限制来避免无限循环:

// 新增带深度参数的重载方法
private void executeCommandsFromFile(String filename, int depth) {
    // 设置最大递归深度,可根据需求调整
    if (depth > 5) {
        System.out.println("已达最大加载深度,停止执行以避免无限循环。");
        return;
    }
    try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
        String line = reader.readLine();
        while (line != null) {
            processCommand(line, depth + 1);
            line = reader.readLine();
        }
        System.out.println("Commands executed from file: " + filename);
    } catch (FileNotFoundException e) {
        System.out.println("File not found: " + filename);
    } catch (IOException e) {
        System.out.println("Error reading file: " + filename);
    }
}

// 原方法调用重载方法,初始深度为0
private void executeCommandsFromFile(String filename) {
    executeCommandsFromFile(filename, 0);
}

// 修改processCommand方法,接收深度参数
public void processCommand(String command, int depth) {
    if (command.equals("save")) {
        saveCommandsToFile("commands.txt");
    } else if (command.equals("load")) {
        executeCommandsFromFile("commands.txt", depth);
    } else {
        // 处理海龟的其他命令(如move、turn等)
        // ...
    }
}

// 保留原processCommand重载,供用户直接输入命令调用
public void processCommand(String command) {
    processCommand(command, 0);
}

额外建议

如果你的saveCommandsToFile方法会将执行过的命令写入文件,确保写入的命令列表中不包含load和save,否则加载时仍会触发循环。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 16:42:50