这是一个递归程序,它提供了错误的输出,请帮助我。

huangapple go评论63阅读模式
英文:

Here is a recursive program that gives wrong output, please help me

问题

这是一段关于在书中看到的程序的描述,它包括一个名为printOut的程序,用于输出整数。程序使用递归来实现,当n大于或等于10时,它将继续调用printOut,否则它将调用printDigit来打印最后一位数字。

书中提供的示例代码是Java代码,其中定义了printOut方法,但书中没有提供printDigit方法的代码。

然后,您提供了自己的代码示例,其中定义了一个Print类,包含printOutprintDigit方法。您运行代码后发现,终端只输出数字7,而您认为问题可能出在printDigit方法只调用了一次。

您希望知道如何修改代码以使其正常工作。

英文:

I saw a program like this in a book:

Here is a program: print out integers. Here you have a positive integer n and want to print it out. The name of the program is printOut(n). Suppose there is an existing routine that will only process a single number and output it to the terminal .The routine that prints a single digit is named printDigit.

The program requires the use of recursion.

The sample code given in the book is:

    public static void printOut(int n)
    {
        if (n >= 10)
        {
            printOut(n / 10);
        }
        else
        {
            printDigit(n % 10);
        }
    }

The code of the routine printDigit is not given in the book. After thinking, I think the program should be like this:

public class PrintOutTest
{
    public static void main(String[] args)
    {
        Print.printOut(76234);
    }
}

class Print
{
    public static void printOut(int n)
    {
        if (n >= 10)
        {
            printOut(n / 10);
        }
        else
        {
            printDigit(n % 10);
        }
    }

    static void printDigit(int n)
    {
        System.out.print(n); //7
    }
}

But when I run it, the terminal only outputs the number 7.

I took a closer look and found that the printDigit method only entered once, which should be the problem. But I don’t know how to modify it. Please ask if you can help me. Thanks

答案1

得分: 1

从左到右(最高有效位到最低有效位)输出数字。

获取最右边的数字(即 n % 10)比获取最左边的数字(即 n 除以...某个幂次的 10)更容易。所以递归将采用以下形式:

打印一个数字:
  如果数字多于一位
    递归调用,去掉最右边的数字
  打印最右边的数字

更具体地说:

打印数字 n:
  如果 n > 9
    打印数字 n / 10
  打印数字 n % 10
英文:

You need to emit the digits from left to right (most significant to least significant).

It is easier to get the rightmost digit (this is n % 10) than the leftmost one (this is n divided by... some power of 10). So the recursion will take this shape:

To print out a number:
  if longer than one digit
    recurse on the number without its rightmost digit
  print out the rightmost digit

and more precisely,

To print_number n:
  if n > 9
    print_number n / 10
  print_digit n % 10

答案2

得分: 0

PrintDigit 仅在 n 小于 10 时被调用,所以这只会发生一次...

在进行递归调用时,你还需要调用 PrintDigit。所以:

    public static void printOut(int n)
    {
        if (n >= 10)
        {
            printOut(n / 10);
        }
        printDigit(n % 10);
    }
英文:

PrintDigit is only called when n is less than 10, so that happens only once...

You need to also call PrintDigit when you make the recursive call. So:

    public static void printOut(int n)
    {
        if (n >= 10)
        {
            printOut(n / 10);
        }
        printDigit(n % 10);
    }

答案3

得分: 0

在每次迭代中,数字都会除以10。例如,对于你的示例76234 > 7623 > 762 > 76 > 7,现在7小于10,所以递归函数调用了打印方法,然后打印了7。

下面是一个修复问题的示例。

class Print
{
    public static void printOut(int n)
    {
        if (n == 0) return ;
        printOut(n / 10);
        printDigit(n % 10);
    }

    static void printDigit(int n)
    {
        System.out.print(n); //7
    }
}
英文:

In each iteration, the number goes diving by 10
for your example 76234 > 7623 > 762 > 76 > 7 and now 7 is smaller than 10 so the recursive function called the print method and 7 gets printed.

below is an example to fix the problem.

class Print
{
    public static void printOut(int n)
    {
        if (n == 0) return ;
        printOut(n / 10);
        printDigit(n % 10);
    }

    static void printDigit(int n)
    {
        System.out.print(n); //7
    }
}

答案4

得分: 0

你的printDigit函数没问题。只是不要用else块包裹它:

public static void printOut(int n)
{
    if (n >= 10)
    {
        printOut(n / 10);
    }
    printDigit(n % 10);
}

另外,我个人不喜欢从书中学习任何编程语言,因为它们会过时,作者经常不擅长算法。而且一旦出错,就没有机会重新出版一本书,但在Github上有问题、拉取请求、评论,代码也是可编辑和可分叉的。

英文:

Your printDigit function is OK. Just dont wrap it with else block:

    public static void printOut(int n)
    {
        if (n >= 10)
        {
            printOut(n / 10);
        }
        printDigit(n % 10);
    }

And also, I personally don't prefer to learn any programmin language from books cause they gets outdated and writers often suck at algorythms. And once they make a mistake there is no chance to republish a book but in Github there is issues, pull requests, comments, and code is editable also forkable.

huangapple
  • 本文由 发表于 2023年2月10日 05:49:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定