简体   繁体   English

java递归打印相反

[英]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:你错过了几件事:

  • You first want to print pounds, then starts, then new-line and then repeat the process您首先要打印磅,然后开始,然后换行,然后重复该过程
  • The number of stars printed on each line is actually "the initial number passed - level"每行所印的星数,其实就是“通过的初始数-级”
  • You need these two numbers in recursion()您需要在recursion()这两个数字
  • You want to repeat the process even when n is 0 .即使n0您也想重复该过程。

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:几个补充:

  • Use naming standards.使用命名标准。 Class-names should start with capital letter recursion is not a good name.类名应该以大写字母开头recursion不是一个好名字。
  • Also it is bad practice to have class and method with exactly same name.同样,使用完全相同的名称的类和方法也是不好的做法。 So consider renaming class 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM