收到错误消息 - "java.base/java.lang.StringConcatHelper.simpleConcat(StringConcatHelper.java:421)"
Getting an error message - "java.base/java.lang.StringConcatHelper.simpleConcat(StringConcatHelper.java:421)"
大家好,我正在制作一个“生成所有括号”的程序,我得到的错误消息是:-"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;
} }
您的递归需要一个退出条件。也许是这个?
if (open == 0 && close == 0){
System.out.println(op);
--> return;
}
大家好,我正在制作一个“生成所有括号”的程序,我得到的错误消息是:-"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;
} }
您的递归需要一个退出条件。也许是这个?
if (open == 0 && close == 0){
System.out.println(op);
--> return;
}