DEV Community

Discussion on: Daily Challenge #88 - Recursive Ninjas

Collapse
 
edh_developer profile image
edh_developer

Java

        public static void chirp(int i)
        {
                if (i == 1)
                        System.out.println("chirp");
                else if (i > 1) {
                        System.out.print("chirp-");
                        chirp(i - 1);
                }
        }

Note that this assumes a few things:

  • We want a carriage return at the end of the string.
  • The correct response to chirp(i) for i < 1 is silence.
  • The value of i is not going to be large enough for us to be worried about blowing the stack.