【发布时间】:2020-12-03 10:11:16
【问题描述】:
大家好,我正在制作一个“生成所有括号”的程序,我收到错误消息:-"java.base/java.lang.StringConcatHelper.simpleConcat(StringConcatHelper.java:421) "
与此同时,我收到堆栈溢出错误
我正在附加一个包含此错误的代码,请查看它并在需要时告诉我修改:-
public class j{
public static void main(String[] args){
int n = 2;
int open = n;
int close = n;
String op = " ";
findAns(op, open, close);
}
private static void findAns(String op, int open, int close){
if (open == 0 && close == 0){
System.out.println(op);
}
if (open == close){
String op1 = op + "(";
findAns(op1, open - 1, close);
return;
}
if (open != 0){
String op1 = op + "(";
open = open - 1;
findAns(op1, open, close);
}
String op1 = op + ")";
close = close - 1;
findAns(op1, open, close);
return;
} }
【问题讨论】:
-
这不是错误消息,而是来自堆栈跟踪的堆栈帧。发布整个堆栈跟踪。
-
好吧,反正这是堆栈溢出。发生这种情况是因为您的递归调用永远不会终止。
-
先生,您能告诉我哪里出错了!
-
你试过调试你的代码吗?通过这样做,您可以清楚地看到问题出在哪里。
标签: java string algorithm data-structures stack-overflow