英文:
Getting an error message - "java.base/java.lang.StringConcatHelper.simpleConcat(StringConcatHelper.java:421)"
问题
抱歉,您的请求不太清楚。您想要我翻译哪部分内容?请提供要翻译的具体文本或句子。
英文:
Hello Guys I was making a program of "Generate all parenthesis" and I got the error message as:-"java.base/java.lang.StringConcatHelper.simpleConcat(StringConcatHelper.java:421)"
along with this I am getting error of stack overflow
I am attaching a code with this error please see to it and tell me the modification if needed:-
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;
} }
答案1
得分: 1
你需要为递归添加一个退出条件。也许是这样?
if (open == 0 && close == 0){
System.out.println(op);
return;
}
英文:
You need an exit condition for your recursion. Maybe this?
if (open == 0 && close == 0){
System.out.println(op);
--> return;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论