递归号码打印

发布于 2024-09-30 06:24:48 字数 401 浏览 3 评论 0原文

如果给我们数字5,它需要像1,2,3,4,5一样在控制台上打印出来。这需要递归地完成。 (最好是 Java)

如果有人想知道这些不是家庭作业问题。我正在为一周后的期中考试进行练习。

很抱歉没有发布我的作品。我正在做类似下面的事情:但是对在哪里打印其余数字以及如何停止使用 (n - 1) 递归调用该方法感到困惑。雅各布的帖子帮助了我。感谢所有提供帮助的人。

public void writeNums(int n) {
    if (n < 1)
        throw new IllegalArgumentException();
    if (n == 1) {
        System.out.print("1, ");
    }
    writeNums(n - 1);

If the number 5 is given to us, it needs to be printed out on the console like 1, 2, 3, 4, 5. This needs to be done recursively. (Java preferably)

In case anyone is wondering these are not homework questions. I am practicing for my midterm a week from now.

Sorry about not posting my work. I was doing something like the below: but getting confused with where to print the rest of the number, and how to stop recursively calling the method with (n - 1). Jacob with his post helped me out. Thanks to everyone who helped.

public void writeNums(int n) {
    if (n < 1)
        throw new IllegalArgumentException();
    if (n == 1) {
        System.out.print("1, ");
    }
    writeNums(n - 1);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

就此别过 2024-10-07 06:24:48

我们不会为您编写代码,但递归的工作方式是您有一个调用自身的函数,传入一些随每次调用而变化的参数。它需要处理“基本情况”,其中函数执行某些操作但不再需要调用自身,并且还处理“一般情况”,其中函数既执行某些操作又调用自身来完成需要完成的任何操作。所以:

function printNumbersUpTo( current )
    if current is 1, print 1 and return. //base case
    otherwise, call printNumbersUpTo( current - 1), then print current, and return. //general case

We're not going to write your code for you, but the way recursion works is that you have a function that calls itself, passing in some parameter that changes for each call. It needs to handle the "base case" in which the function does something but doesn't need to call itself anymore, and also handle the "general case" where the function both does something and calls itself to complete whatever needs to be done. So:

function printNumbersUpTo( current )
    if current is 1, print 1 and return. //base case
    otherwise, call printNumbersUpTo( current - 1), then print current, and return. //general case
二手情话 2024-10-07 06:24:48

让我们首先编写一个函数,该函数的作用不大,但它是基本递归函数的一些明显的骨架。

void Print(int num)
{
    if (num <= 0)
        return;

    Print(num - 1);
}

现在尝试考虑将实际打印添加到控制台的位置,以便数字以正确的顺序出现。

Lets start by writing a function that doesn't do much but it is some obvious skeleton for basic recursive function

void Print(int num)
{
    if (num <= 0)
        return;

    Print(num - 1);
}

Now try to think where to add the atual print to console in order for the number to appear in the correct order.

护你周全 2024-10-07 06:24:48

如果您遇到这个问题,您可能想回顾一下递归的一般工作原理:http:// /en.wikipedia.org/wiki/Recursion_%28computer_science%29

思考应该如何处理基本情况,并思考应该如何处理一般情况。如果做得正确,通常会感觉非常简单。

If you struggled with this problem, you probably want to review how recursion in general works: http://en.wikipedia.org/wiki/Recursion_%28computer_science%29

Think about how the base case should be handled, and think about how the general case should be handled. When done right it often it can feel remarkably simple.

ˇ宁静的妩媚 2024-10-07 06:24:48
private static String Print(int num) {
    if (num <= 1) // base case
        return Integer.toString(num);
    else
        return Print(num - 1) + ", " + num;
    }
}
private static String Print(int num) {
    if (num <= 1) // base case
        return Integer.toString(num);
    else
        return Print(num - 1) + ", " + num;
    }
}
旧夏天 2024-10-07 06:24:48

你可以非常简单地实现它,没有这样特殊的逻辑,唯一的事情是你必须决定打印机制首先打印哪一个,通过我的代码你将能够理解

public static void print(int n) {
        if (n <= 0) {
            return;
        }

        print(n - 1);
        System.out.print(n+ " ");
    }

输入:5
输出:1,2,3,4,5

you can achive it very simply, no such special logic only thing is you have to decide printing machanism which one to print first , go through my code you will be able to understand

public static void print(int n) {
        if (n <= 0) {
            return;
        }

        print(n - 1);
        System.out.print(n+ " ");
    }

Input : 5
output : 1,2,3,4,5

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文