This question already has answers here:
What is a StackOverflowError?
                                
                                    (14个回答)
                                
                        
                                12个月前关闭。
            
                    
我目前正在编写一个程序,该程序将计算在河内塔问题中需要采取的措施。我需要将所有移动写入输出的.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();
        }
    }
}

最佳答案

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


或添加另一个中断条件

基本上你是用n进入负值

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

10-08 17:13