Java triangle of n height using recursion

前端 未结 2 1775
暖寄归人
暖寄归人 2021-01-28 01:39

Assume the availability of a method named makeLine that can be passed a non-negative integer n and a character c and return a String consisting of n identic

2条回答
  •  后悔当初
    2021-01-28 02:01

    I won't give you the answer, but hopefully this gives you some hints. A recursive method is something that solves a problem by calling itself to solve a smaller version of the same problem. In your case, the problem is to print this (I've put b where blanks belong):

    OOOOO
    bOOO
    bbO
    

    You're printing the first line, then solving a smaller version of the same problem, which is to print a smaller triangle:

    bOOO
    bbO
    

    The problem is that this smaller version isn't quite the same; it has to have extra spaces before each line. How much extra space? Well, that's why the instructor said "use k to your advantage". How could you call the method recursively, and use k to tell it to display extra spaces?

提交回复
热议问题