简体   繁体   English

用递归重复一个字符串,初学者

[英]Repeating a string with recursion, beginner

I'm supposed to write a simple method that returns given string for given amount, seperated by comma (and no comma in the end), with recursion.我应该编写一个简单的方法,返回给定数量的给定字符串,用逗号分隔(最后没有逗号),并带有递归。 If there are less than two counts, the return is empty string "".如果计数少于两个,则返回空字符串“”。

final static String COMMA = ", ";
public static String replicate(String s, int count) {
    String answer = "";
    if (count < 2) {
        return answer;
    }
    else {
        answer = s + COMMA + replicate(s, (count - 1));
        return answer;
    }
}

If I put s = cat and count = 5, I get cat, cat, cat, cat, - One short what I need.如果我把 s = cat 和 count = 5,我得到cat, cat, cat, cat, - 一个我需要的短。 I'm at my wits end here what to do, to get proper amount of repeats here without the comma at the end.我在这里不知所措,要在这里得到适当数量的重复,最后没有逗号。

EDIT: Clearly I failed to communicate, that the method SHOULD return an empty string, if the count is two or less.编辑:显然我未能传达,如果计数为两个或更少,该方法应该返回一个空字符串。 Sorry for the lack of clarity there.很抱歉那里不够清晰。

You're extremely close!你非常接近! When you hit your base case count < 2 , instead of returning an empty string, you can return your input string.当您达到基本情况count < 2 ,您可以返回输入字符串而不是返回空字符串。 Be sure to check that length isn't 0, too.一定要检查长度也不为 0。

EDIT: from information in the comments, you want to return an empty string for any counts less than or equal to two.编辑:根据评论中的信息,您希望为任何小于或等于 2 的计数返回一个空字符串。 Odd requirement, but this will fit that issue;奇怪的要求,但这将适合该问题; it uses three as a base case instead of two, returning three s inputs concatenated together.它使用三个作为基本情况而不是两个,返回连接在一起的三个s输入。

  final static String COMMA = ", ";
  public static String replicate(String s, int count) {
      String answer = "";
      if(count <= 2) {
        return answer;
      }
      if(count == 3) {
        return (s + COMMA + s + COMMA + s);
      }
      else {
          answer = s + COMMA + replicate(s, (count - 1));
          return answer;
      }
  }

Demo演示

Every element is there, just a bit unreadable code.每个元素都在那里,只是有点不可读的代码。

  • If count is 0, "".如果计数为 0,则为“”。
  • If one, s itself.如果是一,则是 s 本身。
  • Otherwise s, + recursive result.否则 s, + 递归结果。

So:所以:

public static String replicate(String s, int count) {
    if (count <= 0) {
        return "";
    } else if (count == 1) {
        return s;
    } else {
        return s + COMMA + replicate(s, count - 1);
    }
}

What lead to mental failure was the variable answer, and the two cases of < 2 .导致精神失败的是可变答案,以及< 2的两种情况。

public static String replicate(String s, int count) {

    if (count >= 2) {
        s = s + COMMA + replicate(s, --count);
    }
    return s;
}

replicate("hii", 4) --> return "hii,hii, hii, hii"复制(“hii”,4)-->返回“hii,hii,hii,hii”

COMMA + replicate("hii", 3) --> return "hii, hii, hii" COMMA + replicate("hii", 2) --> return "hii, hii" COMMA + replicate("hii", 1) --> return "hii" COMMA + replica("hii", 3) --> 返回 "hii, hii, hii" COMMA + replica("hii", 2) --> 返回 "hii, hii" COMMA + replica("hii", 1) --> 返回 "hii"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM