简单递归问题返回方式

时间:2011-12-16 00:46:19

标签: java recursion

public class recursionExcercise4
{

  public static void main (String[] args)
  {
    boolean statement=false;
    String ch="";
    String a="I am bubbles who is a little slugger and loves apple and yellow."
    BacktoBacks(a,ch,statement);
  }

  public static void BacktoBacks(String sentence, String ch, boolean statement)
  {
    String newLine="",word="";
    System.out.print(sentence.charAt(0));
    if(sentence.charAt(0)=='.') System.out.println();

    if(sentence.length()>1)
    {
      int num=sentence.substring(1).indexOf(" ");
      word = sentence.substring(0,num);
      System.out.println(word);
    }
    BacktoBacks(newLine,ch,statement);
  }
}

这就是代码。

if语句循环中的行是由我添加的,因此您可以更改它,但不能更改任何其他内容。顶部的if语句必须保留在那里。此外,我试图避免循环,因为它太容易了。有什么办法吗?我尝试过但需要帮助。

目标是打印出具有双字母的字符串中的单词。这也应该向后打印。像这样:

黄色 苹果 猛男 小 气泡

非常感谢您的帮助。 谢谢!

1 个答案:

答案 0 :(得分:2)

所以你需要一个函数来检查一个单词是否包含双字母,

public boolean hasDoubleLetters(String word){
    // test
}

并根据其结果,在递归调用后打印该单词。你必须将正确的参数传递给递归调用。