printStars method with recursion

Here are my instructions: Write a recursive method called printStar(int n) which will print the following when n=4:

****
***
**
*
*
**
***
****

This is what I did so far:

public class Stars{
   public static void main (String[] args){
      printStars(4);
   }

   public static void printStars(int count){
      if (count==0){
         System.out.println("");
      }
      else{
         System.out.print("*");
         printStars(count-1);
      }
   }
}

My attempt only prints one line of the given number of stars. I don't know how to print multiple lines with only one call of the method. Any help is appreciated.

3 answers

  • answered 2018-05-16 04:40 Dan

    Your program is incorrect, see the below program and analyze it -

    public class Stars{
       public static void main (String[] args){
          printStars(4);
       }
    
       public static void printStars(int count){
          if (count==0){
            // System.out.println("");
          }
          else{
              int tempCount = count;
              while(tempCount>0){
                  System.out.print("*");
                  tempCount--;
              }
             System.out.println();
    
             printStars(count-1);
    
             while(tempCount<count){
              System.out.print("*");
              tempCount++;
          }
          System.out.println();
          }
       }
    } 
    

    Explanation - I am using two while loops to print stars, first to print the start in asc order and second to print in reverse order. Using variable tempCount to get the current counter.

  • answered 2018-05-16 04:51 user7

    Try this

    public static void printStars(int count){
        if (count == 0){
            return;
        }
        printStarNTimes(count);
        printStars(count-1);
        printStarNTimes(count);
    }
    private static void printStarNTimes(int n) {
        for (int i = 0; i < n; i++) {
            System.out.print("*");
        }
        System.out.println();
    }
    

    Logic is in printing star count times in the current call and when the recursive call returns.

  • answered 2018-05-16 06:28 prms

    This one uses tail recursion, is split into helper function, and uses loop only to get the character as required.

    public class JavaR {
        public static void main(String[] args) {
            System.out.println(star(4, new StringBuilder()));
        }
    
        public static StringBuilder star(int times, StringBuilder builder) {
            if (times < -4) return builder;
            else if (times != 0) {
                builder.append(getNTimes(Math.abs(times)));
                return star(times - 1, builder);
            } else {
                return star(times - 1, builder);
    
            }
        }
    
        public static String getNTimes(int count) {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < count; i++) {
                builder.append("*");
            }
            return builder.append("\n").toString();
        }
    
    }