[英]java recursion print opposite
I am using recursion in Java, the output should look like我在 Java 中使用递归,输出应该像
###
##*
#**
***
This is how I'm doing it right now:这就是我现在的做法:
public void recursion(int n){
if (n==0) {
return;
}
else {
System.out.print(number_pound(n));
System.out.println();
recursion(n-1);
System.out.print(stars(n));
System.out.println();
}
}
private String number_pound(int level) {
String s = "";
for(int i=0;i<level;i++)
s+="#";
return s;
}
private String stars(int level) {
String s = "";
for (int i = 0; i < level; i++)
s+= "*";
return s;
}
public static void main(String[] args) {
recursion rec = new recursion();
rec.recursion(3);
}
which gives me the result in this way:这给了我这样的结果:
###
##
#
*
**
***
I know that the problem with my code is that I have the System.out.println()to separate two parts, but without the printline statement, the number pound will be jammed in the same line.我知道我的代码的问题是我有 System.out.println() 来分隔两个部分,但没有 printline 语句,数字磅将卡在同一行中。
This will do what you are trying to do, I believe.我相信这会做你想做的事。 It will print the number of pounds, then the number of stars, and then the new line.
它将打印磅数,然后是星星数,然后是新行。 You will have to pass an additional length parameter, since you need to specify the length of the output string.
您必须传递一个额外的长度参数,因为您需要指定输出字符串的长度。 You cannot do without the length, I believe.
我相信你不能没有长度。
public void recursion(int n, int length){
if (n<0) {
return;
}
else {
System.out.print(number_pound(n));
System.out.print(stars(length - n));
System.out.println();
recursion(n-1, length);
}
}
So, now your call in the main looks like rec.recursion(3, 3)
所以,现在你在 main 中的调用看起来像
rec.recursion(3, 3)
Moreover I believe it would be much better to do it iteratively than using recursion.此外,我相信迭代执行比使用递归要好得多。
You have missed few things:你错过了几件事:
recursion()
recursion()
这两个数字n
is 0
.n
为0
您也想重复该过程。 This is how it should be:应该是这样:
public void recursion(int max, int n){
if (n<0) {
return;
}
else {
System.out.print(number_pound(n));
System.out.print(stars(max - n));
System.out.println();
recursion(max, n-1);
}
}
private String number_pound(int level) {
String s = "";
for(int i=0;i<level;i++)
s+="#";
return s;
}
private String stars(int level) {
String s = "";
for (int i = 0; i <level; i++)
s+= "*";
return s;
}
public static void main(String[] args) {
recursion rec = new recursion();
rec.recursion(3, 3);
}
Couple of additions:几个补充:
recursion
is not a good name.recursion
不是一个好名字。recursion
.recursion
。public class recursion {
int k;
public void recursion(int n){
if(k==0){
k=n;
}
if (n<0) {
return;
}
else {
System.out.print(number_pound(n)+stars(k-n));
System.out.println();
recursion(n-1);
}
}
private String number_pound(int level) {
String s = "";
for(int i=0;i<level;i++)
s+="#";
return s;
}
private String stars(int level) {
String s = "";
for (int i = 0; i < level; i++)
s+= "*";
return s;
}
public static void main(String[] args) {
recursion rec = new recursion();
rec.recursion(3);
}
} }
A variable length
should represent the max number (3 in your case).可变
length
应表示最大数量(在您的情况下为 3)。 You just need to keep displaying n
#s and length - n
stars until n becomes less than 0.您只需要继续显示
n
#s 和length - n
颗星,直到 n 小于 0。
public void recursion(int n, int length){
if(n>=0){
System.out.print(number_pound(n));
System.out.print(stars(length - n)+"\n");
recursion(n-1, length);
}
}
Call recursion(3,3)
to get the output.调用
recursion(3,3)
以获取输出。 If you want the signature of the recursion() method to be (int n)
only, then you could use a global static variable to refer to the max number to which you assign the max number in the main method before calling recursion().如果您希望 recursion() 方法的签名仅为
(int n)
,那么您可以使用全局静态变量来引用在调用 recursion() 之前在主方法中分配最大数量的最大数量。
int length = 3;
private List<String> newList(String x, int length) {
return Arrays.asList(new String[length]).stream().map(s -> x)
.collect(Collectors.toList());
}
private void recurSymbol(String x, String y, int length) {
List<String> list = newList(x, length);
list.addAll(newList(y, this.length - length));
System.out.println(String.join("", list));
if(length > 0) recurSymbol(x, y, length - 1);
}
public void recursion(String x, String y) {
recurSymbol(x, y, length);
}
Then just call something like然后只需调用类似的东西
recursion("#", "*");
Obviously, length can be any int 0 or greater.显然,长度可以是任何整数 0 或更大。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.