Getting an error message – "java.base/java.lang.StringConcatHelper.simpleConcat(StringConcatHelper.java:421)"

huangapple go评论98阅读模式
英文:

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:-

  1. public class j{
  2. public static void main(String[] args){
  3. int n = 2;
  4. int open = n;
  5. int close = n;
  6. String op = " ";
  7. findAns(op, open, close);
  8. }
  9. private static void findAns(String op, int open, int close){
  10. if (open == 0 && close == 0){
  11. System.out.println(op);
  12. }
  13. if (open == close){
  14. String op1 = op + "(";
  15. findAns(op1, open - 1, close);
  16. return;
  17. }
  18. if (open != 0){
  19. String op1 = op + "(";
  20. open = open - 1;
  21. findAns(op1, open, close);
  22. }
  23. String op1 = op + ")";
  24. close = close - 1;
  25. findAns(op1, open, close);
  26. return;
  27. } }

答案1

得分: 1

你需要为递归添加一个退出条件。也许是这样?

  1. if (open == 0 && close == 0){
  2. System.out.println(op);
  3. return;
  4. }
英文:

You need an exit condition for your recursion. Maybe this?

  1. if (open == 0 && close == 0){
  2. System.out.println(op);
  3. --> return;
  4. }

huangapple
  • 本文由 发表于 2020年8月13日 22:21:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63397229.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定