简体   繁体   English

使用递归的Java乘法

[英]Java multiplication using Recursion

I am writing a simple code in Java that is using recursion. 我正在使用递归的Java语言编写一个简单的代码。 I want to show the product of two numbers that a user will enter. 我想显示用户将输入的两个数字的乘积。 I managed to do that using recursion, but stuck at the point where I want to show that the product could be written as (example) 10*5 = 5+5+5+5+5+5+5+5+5+5 (10 times), or 12*3 = 3+3+3+3+3+3+3+3+3+3+3+3 (12 times). 我设法使用递归做到了这一点,但停留在我想证明该产品可以写为(例如)10 * 5 = 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次)。 Here is my code so far. 到目前为止,这是我的代码。 In the code i put a comment where it should be written (example). 在代码中,我在应该写的地方添加了注释(示例)。 Thanks. 谢谢。

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));
        }
    }

  }

}

A StringBuilder should be defiend as StringBuilder应该被视为

StringBuilder buf = new StringBuilder (a);

Pass this StringBuilder paramater into multiRec 将此StringBuilder参数传递给multiRec

and then change multiRec to be 然后将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));
        }
    }

  }

}

Then when completed simply printout its value 然后,完成后只需打印其值

    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));
            }
        }

      }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM