Java x到y的幂的递推方法
我2,3得到4。这是什么原因造成的Java x到y的幂的递推方法,java,recursion,Java,Recursion,我2,3得到4。这是什么原因造成的 public static int basepowerexp(int base, int exp) { if (exp == 0) { return 1; } else { return base * basepowerexp(exp - 1, base); } } public static void bpe(int base, int exp) { System.out.println("T
public static int basepowerexp(int base, int exp) {
if (exp == 0) {
return 1;
} else {
return base * basepowerexp(exp - 1, base);
}
}
public static void bpe(int base, int exp) {
System.out.println("The answer to " + base + " to the power of " + exp
+ " is " + power(base));
}
我认为这与:
return base * basepowerexp(exp - 1, base);
但我想不出来,我试过其他的变体 您必须更改函数调用中参数的顺序。这:
return base * basepowerexp(exp-1,base);
为此:
return base * basepowerexp(base, exp - 1);
但我想知道,正如你所说,你2,3得到4!因为我测试的结果是0
编辑:
正如您所提到的,问题仍然存在,我将在您发现问题时提供一个工作代码:
public class MyClass {
public static void main(String[] args) {
System.out.println(basepowerexp(2, 3));
}
public static int basepowerexp(int base, int exp){
if (exp == 0) {
return 1;
} else {
return base * basepowerexp(base, exp - 1);
}
}
}
正是这个代码,为我打印8
。
如果问题存在,请告诉我。您必须更改函数调用中参数的顺序。这:
return base * basepowerexp(exp-1,base);
为此:
return base * basepowerexp(base, exp - 1);
但我想知道,正如你所说,你2,3得到4!因为我测试的结果是0
编辑:
正如您所提到的,问题仍然存在,我将在您发现问题时提供一个工作代码:
public class MyClass {
public static void main(String[] args) {
System.out.println(basepowerexp(2, 3));
}
public static int basepowerexp(int base, int exp){
if (exp == 0) {
return 1;
} else {
return base * basepowerexp(base, exp - 1);
}
}
}
正是这个代码,为我打印8
。
如果问题存在,请告诉我。当指数为1时,应达到基本情况。看看我的代码
public class test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//Enter base;
int base = in.nextInt();
//Enter exponent
int exp = in.nextInt();
System.out.println("The answer to " + base + " to the power of "
+ exp + " is " + basepowerexp(base, exp));
}
public static int basepowerexp(int base, int exp) {
if(exp >= 1)
return base * basepowerexp(base, exp-1);
else
return 1;
}
}
当指数为1时,应达到基本情况。看看我的代码
public class test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//Enter base;
int base = in.nextInt();
//Enter exponent
int exp = in.nextInt();
System.out.println("The answer to " + base + " to the power of "
+ exp + " is " + basepowerexp(base, exp));
}
public static int basepowerexp(int base, int exp) {
if(exp >= 1)
return base * basepowerexp(base, exp-1);
else
return 1;
}
}
您在递归调用中反转了指数和基数。参数的顺序在调用中翻转…@rgetman即使我这样做了,返回base*basepowerexp(base-1,exp);“我还是得到了四个答案。”亚当霍利根想了想。你不能减少基数。你递减指数。这正是指数运算的定义。如果使用调试器,可能需要几秒钟才能找到错误。您在递归调用中反转了指数和基数。参数的顺序在调用中翻转…@rgetman即使我这样做,也返回base*basepowerexp(base-1,exp);“我还是得到了四个答案。”亚当霍利根想了想。你不能减少基数。你递减指数。这正是指数运算的定义。如果你使用了调试器,你会花上几秒钟找到你的错误。这仍然给了我2,3的4。在main方法中,我像这样运行它:bpe(2,3);我将编辑我的答案,并用方法和函数调用编写整个类,然后您会发现其中的差异。;)在这里,我有System.out.println(“对“+base+”和“+exp+”的幂的回答是”+power(base,exp));它应该是System.out.println(“对“+base+”的幂“+exp+”的答案是“+basepowerexp(base,exp));:)这仍然给了我2,3的4分。在main方法中,我像这样运行它:bpe(2,3);我将编辑我的答案,并用方法和函数调用编写整个类,然后您会发现其中的差异。;)在这里,我有System.out.println(“对“+base+”和“+exp+”的幂的回答是”+power(base,exp));它应该是System.out.println(“对“+base+”的幂“+exp+”的答案是“+basepowerexp(base,exp));:)