当前位置: 首页 > 知识库问答 >
问题:

输出数模式的Java递归

琴献
2023-03-14

所需输出:

    5
   454
  34543
 2345432
123454321

我如何使用递归来完成这件事呢?我的代码思想是:

public static void main(String[] args)
{
      System.out.println(func(5)); 
}
public static String func(int num)
{
     return num + "" +meth(num-1, num, num-1);
}

public static String meth(int start, int num, int end)
{

    if(start==1)
    {
        return "1";
    }
    System.out.println(start+num+end);

    return meth(start-1, num, end-1);
}

我对在if语句和system.out.println()中返回什么感到困惑,因为数字5不会随着它的停留而减少/增加例如,它将垂直停留5,我该如何处理这个问题呢?我的代码更多的是一个说明,只是为了证明我在做这件事。

共有2个答案

穆单鹗
2023-03-14

我认为只需传递num by参数和前一个字符串(即前一行):

private static String meth(int num,String previous) {

     String space="";
    for(int i=0; i<num; i++) space+=" ";
    //If number is negative, return empty String
    if(num<=0) return "";

    //if number is 1, we need to check if previous string is empty or not, because if is empty we need then umber only once, otherwise we need to add to the string
    else if(num==1){
        if(!previous.isEmpty()) return space+num+previous+num;
        else return space+num+"";
    }

    //Here is checked if previous is empty and we do the same as before with number one
    String currentRow=previous.isEmpty()? String.valueOf(num) : num+previous+num;

    //We return the current row (with the current number), and we add the next row (or tree level) passing the number-1 and the row we have
    return space+currentRow+"\n"+meth(num-1,currentRow);

}
滕夜洛
2023-03-14

也许这就是你要找的:

public class Main {
    public static void main(String[] args) {
        startRecursion(5);
    }

    private static void startRecursion(int number) {
        String aligner = "";
        for (int i = 0; i < number - 1; i++) {
            aligner += " ";
        }
        recursion(String.valueOf(number), number, number, aligner);
    }

    private static void recursion(String value, int startNumber, int lastNumber, String aligner) {
        if (lastNumber < 1) {
            return;
        }

        if (lastNumber != startNumber) {
            value = lastNumber + value + lastNumber;
        }

        System.out.println(aligner + value);

        if (!aligner.isEmpty()) {
            aligner = aligner.substring(0, aligner.length() - 1);
        }

        recursion(value, startNumber, lastNumber - 1, aligner);
    }
}

打印:

    5
   454
  34543
 2345432
123454321
 类似资料:
  • 我已经想出了如何创建一个数组,其中的输出如下所示: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 如果用户输入4表示4x4阵列。我的问题是,我如何操作这段代码,让它按照这样的顺序输出数组 1 2 3 4 8 7 6 5 9 10 11 12 16 15 14 13 每隔一行都是“向后的” `导入java。util。扫描仪;导入java。util。阵列;导入java。

  • 使用以下代码: 为什么这个输出: 而不是我所期望的,那就是: 我讨厌递归。我讨厌递归。我讨厌递归。谢谢

  • 问题内容: 我想知道Java是否有某种类可以帮助输出格式化。我知道在C ++中,在iomanip中,有一个方法调用setw。我想知道Java是否有与此类似的东西。 问题答案: 看看java.util.Formatter。 String.format()提供了一个方便的包装器。 例如(从链接上的示例修改): 它超越了C的?printf格式。例如,它支持可选的语言环境,并且格式符号可以通过显式索引而不

  • 本文向大家介绍IOS中(Xcode) DEBUG模式(RELEASE模式)控制NSLog输出,NSLog输出方式,包括了IOS中(Xcode) DEBUG模式(RELEASE模式)控制NSLog输出,NSLog输出方式的使用技巧和注意事项,需要的朋友参考一下 IOS中(Xcode) DEBUG模式(RELEASE模式)   在开发IOS程序过程中,经常需要用到NSLog输出一些信息,甚至有的开发过

  • 假设在一个在线商店的应用系统中需要一个HTML页面,和下面这个页面类似: <html> <head> <title>Welcome!</title> </head> <body> <h1>Welcome John Doe!</h1> <p>Our latest product: <a href="products/greenmouse.html">green mouse</a>!

  • 问题内容: 使用第三方API,我观察到以下内容。 而不是使用 它使用类似 我得到分配的“输出”字符串。 我很好奇实现这种功能的原因。使用此类输出参数的优点是什么? 问题答案: 在您的示例中有不对的地方。 在上面的程序中,将输出字符串“ foo”, 而不是 “ Hello World”。 某些类型是可变的,在这种情况下,您可以修改传递给函数的对象。对于不可变的类型(例如),您必须构建某种包装类,而可