使用递归的n高度的Java三角形 [英] Java triangle of n height using recursion
问题描述
假设一个名为makeLine的方法的可用性,该方法可以传递一个非负整数n和一个字符c,并返回一个由n个相同字符组成的字符串,这些字符都等于c。编写一个名为printTriangle的方法,它接收两个整数参数n和k。如果n为负,则该方法不起作用。如果n碰巧是偶数,则将其值提高到下一个奇数(例如4 - > 5)。然后,当k的值为零时,该方法打印一个O的SYMMETRIC三角形(大写字母O),如下所示:首先是一行n O,然后是一行n-2 O(缩进一个空格),然后是一行n-4 O(缩进两个空格),依此类推。例如,如果方法收到5,0(或4,0),则会打印:
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 identical characters that are all equal to c. Write a method named printTriangle that receives two integer parameters n and k. If n is negative the method does nothing. If n happens to be an even number, its value is raised to the next odd number (e.g. 4-->5). Then, when k has the value zero, the method prints a SYMMETRIC triangle of O's (the capital letter O) as follows: first a line of n O, followed by a line of n-2 O's (indented by one space), and then a line of n-4 O's (indented by two spaces), and so on. For example, if the method received 5,0 (or 4,0) it would print:
OOOOO
OOO
O
注意:在上面的输出中,第一行在第一个O之前包含0个空格,下一个第1行包含空格,依此类推。
Note: in the above output , the first line contains 0 spaces before the first O, the next line 1 space, and so on.
注意:这些指令说明当k为零时该方法的作用,但程序员可以自行决定当k不为零时它做什么使用它为您的利益。
Note: These instructions state what the method does when k is zero, but it is up to you, the programmer, to determine what it does when k is not zero and use it for your advantage.
该方法不得使用任何类型的循环(for,while,do-while)来完成其工作。该方法应调用makeLine来完成创建不同长度的字符串的任务。
The method must not use a loop of any kind (for, while, do-while) to accomplish its job. The method should invoke makeLine to accomplish the task of creating Strings of varying lengths.
这是我到目前为止所拥有的。我在找出放置间距的位置时遇到了麻烦。我相信它与k有关,但我不太确定。
This is what I have so far. I'm having trouble figuring out where to put the spacing. I believe it has something to do with k, but I'm not quite sure.
public void printTriangle(int n, int k){
if(n < 0)
return;
if(n % 2 == 0)
n++;
if(k == 0){
System.out.println(makeLine(n, 'O'));
printTriangle(n-2, 0);
}
}
推荐答案
我不会给你答案,但希望这会给你一些提示。递归方法通过调用自身来解决较小版本的相同问题来解决问题。在你的情况下,问题是打印这个(我把 b
在空白处所属的地方):
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
问题是这个较小的版本并不完全相同;它必须在每一行之前有额外的空格。多少额外空间?好吧,这就是教师说使用 k
的原因。你如何递归调用该方法,并使用 k
告诉它显示额外的空格?
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?
这篇关于使用递归的n高度的Java三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!