在 Java 中使用递归时如何解决 StackOverflowError?
How do you resolve a StackOverflowError while using recursion in Java?
我目前正在编写一个程序来计算在汉诺塔问题中需要采取的步骤。我需要将所有动作写入输出 .txt 文件。
当我尝试这样做时,我在 towerOfHanoiMoves 方法中的 if 语句的开头保持 Whosebug 错误一次,并在第一次递归调用同一方法时保持多次。
我猜错误与 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 个变量
我目前正在编写一个程序来计算在汉诺塔问题中需要采取的步骤。我需要将所有动作写入输出 .txt 文件。
当我尝试这样做时,我在 towerOfHanoiMoves 方法中的 if 语句的开头保持 Whosebug 错误一次,并在第一次递归调用同一方法时保持多次。
我猜错误与 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 个变量