Create a triangle out of stars using only recursion

前端 未结 10 870
独厮守ぢ
独厮守ぢ 2020-12-06 06:52

I need to to write a method that is called like printTriangle(5);. We need to create an iterative method and a recursive method (without ANY iteration). The out

10条回答
  •  盖世英雄少女心
    2020-12-06 07:22

    I think this should work... untested off the top of my head.

    public void printTriangle(int count)
    {    
        if (count == 0) return;
        printTriangle(count - 1);
        for (int x = 1; x <= count; x++) { 
            System.out.print("*"); 
        }
        System.out.print("\n"); 
    }
    

提交回复
热议问题