小编Sim*_*one的帖子

如何在java中使用递归反转数字序列

我很坚持这段代码,赋值很简单:“给定一个数字 n 或值,在控制台上打印从 n 到 1 和从 1 到 n 的序列(重复数字 1 所以它应该是这样的'5432112345')。

递归本身不是问题,真正的问题是第二部分,因为我不能仅使用任何其他变量 n。我无法在任何地方存储起始值,因为每次我调用该方法时它都会被实现。

这是到目前为止的代码:

public int mirrorRecursive(Integer value){
       if (value < 1){        //in case the given value is less than 1 it won't print anything
            return value;
        }
        if(value == 1){        //in case the value is 1, it will be printed and stop the calling
            System.out.print(value);
        }else{                 //in case the value is not 1 or less, it will print and call again the method
            System.out.print(value);
            mirrorRecursive(--value);
        }

        return …
Run Code Online (Sandbox Code Playgroud)

java recursion numbers sequence

0
推荐指数
1
解决办法
372
查看次数

标签 统计

java ×1

numbers ×1

recursion ×1

sequence ×1