David Wallace 的回答很好,但如果输入是 (1,124) 或 (-1,124),递归深度(方法调用自身的次数)将为 123,效率不高。我建议添加几行来测试 1 或 -1。这是一个完整的例子:
public class Multiply {
public static void main(String[] args) {
System.out.print("product = " + multiply(1, 124) );
}
public static int multiply(int x, int y) {
System.out.println("Multiply called: x = " + x + ", y = " + y);
if (x == 0 || y == 0) {
System.out.println("Zero case: x = " + x + ", y = " + y);
return 0;
}
else if (x == 1 && y > 0) {
return y;
}
else if (y == 1 && x > 0) {
return x;
}
else if ( x == -1 && y > 0) {
return -y;
}
else if ( y == -1 && x > 0) {
return -x;
}
else if( y == -1 ) {
System.out.println("y is == -1");
return -x;
}
else if( x == -1 ) {
System.out.println("x is == -1");
return -y;
}
else if( y < 0 ) {
System.out.println("y is < 0");
return -x + multiply(x, y + 1);
}
else {
System.out.println("Last case: x = " + x + ", y = " + y);
return x + multiply(x, y - 1);
}
}
}
输出:
Multiply called: x = 1, y = 124
product = 124