public static int exponent(int baseNum) {
int temp = baseNum *= baseNum;
return temp * exponent(baseNum);
}
現在上面的方法做N * N爲無窮大,如果我調試它,所以它仍然有效,但我需要這個遞歸方法,因爲我的教練要求我們必須找到給出的指數10倍後停止10.遞推指數的方法
電源的方法必須只有一個參數,這裏的調用指數的一些例子:
System.out.println ("The power of 10 in " + n + " is " +
exponent(n));
所以輸出應該是:
The power of 10 in 2 is 1024
OR
The power of 10 in 5 is 9765625
你的遞歸方法沒有基本的情況! –
如果您需要在十次後停止,則需要爲其遞歸次數設置一個變量。 –
你需要將權力作爲參數傳遞,比如'power(baseName,n);'每次遞歸時減少1。 –