在Java中使用递归时如何解决StackOverflowError? [重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我目前正在编写一个程序来计算在河内塔问题中需要采取的行动。我需要将所有移动写入输出.txt文件。

当我尝试这样做时,我会在towerOfHanoiMoves方法的if语句的开头保持一次StackOverFlow错误,并在第一次递归调用同一方法时多次。

我猜这个错误与outStream有关并且每次都传递给方法,但我不确定。如果是这种情况,我无法弄清楚如何能够在main方法中写入用户给出的相同输出文件。

此外,代码将始终打印来自try catch块中“finally”语句的信息,以及if语句中的outStream语句,但没有其他内容。

我曾尝试在使用towerOfHanoiMoves方法中的outStream.write命令后刷新outStream,但这根本没有帮助。

此外,我已经为BufferedReader,FileReader等导入了所有库,但它们在我的问题中不会正确显示。所以他们只是在你知道的代码中,但他们只是没有出现在这里的代码中。

public class TowerofHanoiRecursive {

    public static void main(String[]args)throws IOException,
    EmptyFile,
    FileNotFoundException {
        int n; //number of disks in tower
        String rodLeft = "A",
        rodRight = "C",
        rodMiddle = "B";
        FileReader inputStream = null;
        FileWriter outputStream = null;
        BufferedReader str = null;

        try {
            outputStream = new FileWriter(args[1]); // output file
            inputStream = new FileReader(args[0]); // input file
            str = new BufferedReader(inputStream);
            String nextLine;
            File newFile = new File(args[0]);

            if (newFile.length() == 0) { //Tests if input file is empty
                throw new EmptyFile("Input file is empty.");

            }
            while ((nextLine = str.readLine()) != null) {
                outputStream.write("----------------------------------------"
                     + "------------------------\n");
                outputStream.write("Number of Disks in Starting Tower = "
                     + nextLine);
                n = Integer.parseInt(nextLine);

                towerOfHanoiMoves(n, rodLeft, rodRight, rodMiddle,
                    outputStream);

            }

        } catch (FileNotFoundException e) {
            outputStream.write("Input file not found.");
            outputStream.flush();
            if (outputStream != null)
                outputStream.close();

        }
        catch (EmptyFile e) {
            outputStream.write(e.getMessage());
            outputStream.flush();
            if (inputStream != null)
                inputStream.close();
            if (outputStream != null)
                outputStream.close();
            str.close();

        }
        finally {
            outputStream.write("");
            outputStream.write("Total time to taken to solve Tower: ");
            outputStream.write("\n\nSuccess!");
            outputStream.flush();

            if (inputStream != null)
                inputStream.close();
            if (outputStream != null)
                outputStream.close();
            str.close();
        }

    }

    public static void towerOfHanoiMoves(int n, String srcRod, String destRod,
        String spareRod, FileWriter outStream) {
        try {
            if (n == 1) {
                outStream.write("\nMove disk 1 from rod " + srcRod + " to rod "
                     + destRod + ".");
            }
            towerOfHanoiMoves(n - 1, srcRod, spareRod, destRod, outStream);
            outStream.write("\nMove disk " + n + " from rod " + srcRod
                 + " to rod " + destRod + ".");
            towerOfHanoiMoves(n - 1, spareRod, destRod, srcRod, outStream);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
java recursion text-files stack-overflow outputstream
1个回答
0
投票
if (n == 1) {
    outStream.write("\nMove disk 1 from rod " + srcRod + " to rod + destRod + ".");
} else {
   ...
}

或添加另一个休息条件

基本上你用n进入负值

PS有一个调试器有助于你逐步完成代码广告检查变量或简单的每一步System.out.println变量

© www.soinside.com 2019 - 2024. All rights reserved.