Java 如何使用加法乘法

Java 如何使用加法乘法,java,Java,有人知道如何在Java中使用加法将两个整数相乘吗? 例如:对于i=4和g=5,代码应该加上4+4+4+4或5+5+5+5 这可能很简单,但我已经坐了几个小时,仍然找不到出路。 提前谢谢 编辑:忘了提了,应该通过递归完成 可以用一个循环来完成 5*4就够了 int result=0; int n=5; int m=4; for (int i=0; i<m; i++) { result+=n; } 它调用自身m次,每次添加n。递归需要注意退出条件,否则会导致严重的无止境循环问题。额外的

有人知道如何在Java中使用加法将两个整数相乘吗? 例如:对于i=4和g=5,代码应该加上4+4+4+4或5+5+5+5

这可能很简单,但我已经坐了几个小时,仍然找不到出路。 提前谢谢


编辑:忘了提了,应该通过递归完成

可以用一个循环来完成

5*4就够了

int result=0;
int n=5;
int m=4;

for (int i=0; i<m; i++)
{
  result+=n;
}
它调用自身
m
次,每次添加
n
。递归需要注意退出条件,否则会导致严重的无止境循环问题。额外的方法用于捕获0个条目,0*任何内容都是0

在递归中,调用n=5,m=4的方法。它自己调用,每次将第二个数字减少1。如果它达到0,它只返回5,否则它返回另一个对自身+5的调用

inti=4;
int i = 4;
int g = 5;

int total = 0;

for (int inc = 0; inc < i; inc++) {
  total += g;
}
int g=5; int-total=0; 对于(int inc=0;inc
如果必须使用递归,我可能会这样做:

public static int multiply(int a, int b) {
    if (a == 0 || b == 0)
        return 0;
    if (a == 1)
        return b;
    if (b == 1)
        return a;
    if (a < 0 && b < 0)
        return multiply(a * -1, b * -1);
    if (a < 0)
        return -1 * multiply(a * -1, b);
    if (b < 0)
        return -1 * multiply(a, b * -1);
    return a + multiply(a, b - 1);
}
公共静态整数乘法(整数a、整数b){
如果(a==0 | | b==0)
返回0;
如果(a==1)
返回b;
如果(b==1)
返回a;
if(a<0&&b<0)
返回乘法(a*-1,b*-1);
if(a<0)
返回-1*乘(a*-1,b);
if(b<0)
返回-1*乘(a,b*-1);
返回a+乘法(a,b-1);
}

让老师高兴的另一件事是写一个测试,比如:

int i = 4;
int g = 5;

int answer = yourMethod(i, g);
int check = i*g;

assert answer == check;
这应该起作用:

public static void main(String[] args) {
    int a = -4, b = 5;
    int product = a * b;
    int ans = add(a, b);

    if (ans == product) {
        System.out.println("Answer " + ans + " is correct.");
    } else {
        System.err.println("Answer " + ans + " is NOT correct. Correct is: " + product);
    }
}

/*
 * Sum of g "copies" of i.
 * E.g. add(3, 5) = 3 + 3 + 3 + 3 + 3
 */
public static int add(int i, int g) {
    // A little optimization. 0 * any number = 0
    if (i == 0 || g == 0) {
        return 0;
    }

    if (g < 0) {
        return add(-i, -g);
    }

    // Since we use recursion we need a base case.
    if (g == 1) {
        return i;
    }

    // Define our problem in terms of the same problem, but of smaller size. 
    return i + add(i, g - 1);
}
publicstaticvoidmain(字符串[]args){
int a=-4,b=5;
int乘积=a*b;
int ans=添加(a,b);
if(ans==产品){
System.out.println(“答案“+ans+”正确”);
}否则{
System.err.println(“答案”+ans+”不正确。正确的是:“+product”);
}
}
/*
*i的g份“副本”之和。
*例如,加(3,5)=3+3+3+3+3
*/
公共静态int-add(int-i,int-g){
//一点优化。0*任意数=0
如果(i==0 | | g==0){
返回0;
}
if(g<0){
返回加法(-i,-g);
}
//因为我们使用递归,所以我们需要一个基本情况。
如果(g==1){
返回i;
}
//用同样的问题来定义我们的问题,但规模较小。
返回i+add(i,g-1);
}


你试过使用循环吗?你为什么要这样做?对不起,忘了提到应该使用递归来完成。这是你的家庭作业。如果是家庭作业,你应该自己解决它,这就是主要思想。或者你可以用循环正确地完成它,那么你就不需要知道结果了……我很傻,我改变主意了你的递归解决方案不正确。什么是乘(0,1)
?对不起,你把它弄坏了。:)现在它将只返回0。我将检查它,感谢您的努力:)这显然是一个家庭作业问题,所以我们真的应该提供工作解决方案吗?他应该解释他正在做什么以及为什么,但我不认为为“家庭作业”问题提供代码有问题。至少它将帮助未来的浏览器。2*(-1)。。。哦有东西坏了吗?哎呀@SilviuBurcea只是编辑来处理底片。是的,现在开始工作了。但是,我认为您不需要if(a<0)检查:)谢谢,我将尝试这一步,并尝试了解每一步:)有人能给我一个建议,如何将其分为两种互补的方法吗?非常感谢,我将检查并尝试这一步!
public static void main(String[] args) {
    int a = -4, b = 5;
    int product = a * b;
    int ans = add(a, b);

    if (ans == product) {
        System.out.println("Answer " + ans + " is correct.");
    } else {
        System.err.println("Answer " + ans + " is NOT correct. Correct is: " + product);
    }
}

/*
 * Sum of g "copies" of i.
 * E.g. add(3, 5) = 3 + 3 + 3 + 3 + 3
 */
public static int add(int i, int g) {
    // A little optimization. 0 * any number = 0
    if (i == 0 || g == 0) {
        return 0;
    }

    if (g < 0) {
        return add(-i, -g);
    }

    // Since we use recursion we need a base case.
    if (g == 1) {
        return i;
    }

    // Define our problem in terms of the same problem, but of smaller size. 
    return i + add(i, g - 1);
}