在Java中使用递归时如何解决StackOverflowError?

时间:2019-03-27 22:43:57

标签: java recursion text-files stack-overflow outputstream

我目前正在编写一个程序,该程序将计算在河内塔问题中需要采取的措施。我需要将所有动作写入输出的.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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

if (n == 1) {
    outStream.write("\nMove disk 1 from rod " + srcRod + " to rod + destRod + ".");
} else {
   ...
}

或添加另一个中断条件

基本上,您将n设为负值

具有调试器的PS可以帮助您逐步查看代码广告,检查变量 或只需在每个步骤中添加System.out.println变量