【问题标题】:Java multiplication using Recursion使用递归的 Java 乘法
【发布时间】:2016-03-03 04:10:09
【问题描述】:

我正在用 Java 编写一个使用递归的简单代码。我想显示用户将输入的两个数字的乘积。我设法使用递归来做到这一点,但停留在我想证明产品可以写为(示例)10*5 = 5+5+5+5+5+5+5+5+5+ 5(10 次),或 12*3 = 3+3+3+3+3+3+3+3+3+3+3+3(12 次)。到目前为止,这是我的代码。在代码中,我在应该写的地方添加了注释(示例)。谢谢。

import java.util.Scanner;

public class RecursiveMultiplication {

public static void main(String[] args) {
    Scanner key = new Scanner(System.in);
    int a, b;
    System.out.print("Enter first number: ");
    a = key.nextInt();
    System.out.print("Enter second number: ");
    b = key.nextInt();
    System.out.println("The product of " + a + " and "
            + b + " is: " + multiRec(a, b));
    System.out.println("It could also be written as: ");   //Here should product be broken into smaller numbers


}

public static int multiRec(int x, int y) {
    if (x == 0 || y == 0) {
        return 0;
    } else {
        if (x == 1) {
            return y;
        } else {
            return x + (multiRec(x, y - 1));
        }
    }

  }

}

【问题讨论】:

  • 您在使用代码时遇到了具体问题吗?
  • 问题是我不知道如何将产品分成更小的数字并将其展示给用户。正如我在示例中所说的(10 * 5 = 50)。现在,我想在屏幕上把那个产品写成 5+5+5+5+5+5+5+5+5+5

标签: java recursion multiplication


【解决方案1】:

StringBuilder 应该被定义为

StringBuilder buf = new StringBuilder (a);

将此 StringBuilder 参数传递给multiRec

然后将multiRec改为

public static int multiRec(int x, int y, StringBuilder buf) {
    if (x == 0 || y == 0) {
        return 0;
    } else {
        if (x == 1) {
            return y;
        } else {
            buf.append (" + ").append (x);
            return x + (multiRec(x, y - 1, buf));
        }
    }

  }

}

然后在完成后简单地打印出它的值

【讨论】:

    【解决方案2】:
        import java.util.Scanner;
    
    public class RecursiveMultiplication {
        public static void main(String[] args) {
            Scanner key = new Scanner(System.in);
           int a , b;
            System.out.print("Enter first number: ");
            a = key.nextInt();
            System.out.print("Enter second number: ");
            b = key.nextInt();
            System.out.printf("%d %s %d %s",a , "*" , b ,"= ");
            System.out.println("\nThe product of " + a + " and "
                    + b + " is: " + multiRec(b, a));
           // System.out.println("It could also be written as: ");   //Here should product be broken into smaller numbers
    
    
        }
    
        public static int multiRec(int x, int y) {
    
            if (x == 0 || y == 0) {
                return 0;
            } else {
                System.out.print(x+" ");
                if (y == 1) {
                    return x;
                } else {
                    System.out.print(" + ");
                    return x + (multiRec(x, y - 1));
                }
            }
    
          }
    }
    

    【讨论】:

    • 这两个例子都帮助我理解了如何实现我所需要的。唯一的事情是格式化。在这两种情况下,最后一个较小数字的末尾都有一个 + 号。这就是我的意思。输出:输入第一个数字:10 输入第二个数字:5 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 10 和 5 的乘积是:50
    • 在上面的代码中,我已经注意到了最后一个“+”符号。
    • 对不起,你做到了。我感谢您的帮助。当我将 10 作为第一个数字并将 5 作为第二个数字时,我现在发现了不同的问题,这是输出:输入第一个数字:10 输入第二个数字:5 10 * 5 = 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 10 和 5 的乘积是:46(不知道为什么是 46)另外,我试着写“10 和 5 的乘积是 50”,然后在下面写“它也可能是写成:5+5+5+5+5+5+5+5+5+5。
    猜你喜欢
    • 1970-01-01
    • 2015-04-21
    • 2016-01-28
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多