現在、5つの方法などを行う再帰的コードを作成しています。私の再帰的メソッドは、2の累乗から2の累乗までのmain()を返して出力します.Xはコマンドライン引数の整数です。Math.powを使用せずに再帰的な方法(2の累乗を2のx乗に戻す)
私はmath.powプロセスを実行できますが、Math.Powを使用せずにどのように行うのですか?
マイコード(問題はpowerCountメソッドです)。
public class TestRun {
/*
* The "main" Method Starts The Program.
* @param args (commandline arguments) are printed to the screen
*/
public static void main (String[]args) {
//initialize variables
int num = 0;
String result = "";
/***********************************
* @ Try-Catch Checks for valid input
* **********************************/
try{
if(args.length == 0)
{
System.out.println("ERROR:NO INPUT FOUND.");
}//end of if
else
{
//convert 1st commandline argument from string to integer
num = Integer.parseInt(args[0]);
//method call to 1st method
result = rowOfAsterisks(num);
//display output of 1st method to screen
System.out.println(result);
//method call to 2nd method
result = forwardCount(num);
//display output of 2nd method to screen
System.out.println(result);
//method call to 3rd method
result = reverseCount(0, num);
//display output of 3rd method to screen
System.out.println(result);
//method call to 4th method
Integer result2 = addCount(0,num);
//display output of 4th method to screen
System.out.println(result2);
//method call to 5th method
result2 = powerCount(1,(int)Math.pow(2,num));
//display output of 5th method to screen
System.out.println(result2);
}//end of else
}//end of try
catch(NumberFormatException e)
{
System.out.print("ERROR: Please input an integer.");
}
}//end of main
/**************************
* Writes x asterisk
* @param num is input of the user and will determine the number of asterisks
* @return x asterisk
***************************/
public static String rowOfAsterisks(int num)
{
//base case
if(num ==1){
return "*";
}
//recursive case
else{
return "*" + rowOfAsterisks(num-1);
}//end of else
}//end of method1
/*****************************
* Counts from number to zero.
* @param num is the input of the user or the argument
* @return string num
*****************************/
public static String forwardCount(int num)
{
//base case
if(num==0){
return num + ", ";
}
//recursive case
else{
return num + ", " + forwardCount(num-1);
}//end of else
}//end of method 2
/***************************************************
* Counts from zero to number.
* @param initialNum is the input/initial integer
* @param finalNum is the final integer
* @return initialNum and finalNum as a String.
****************************************************/
public static String reverseCount(int initialNum, int finalNum)
{
//base case
if(initialNum == finalNum){
return initialNum + ", ";
}
//recursive case
else{
return initialNum + ", " + reverseCount(initialNum +1, finalNum);
}//end of else
}//end of method 3
/*****************************************************
* Adds the number from 0 to number and returns the sum
* @param initialNum is the input/initial integer
* @param finalNum is the final integer
* @return initialNum and finalNum as a String(again)
****************************************************/
public static int addCount(int initialNum, int finalNum)
{
//base case
if(initialNum == finalNum){
return initialNum;
}
//recursive case
else{
return initialNum + addCount(initialNum + 1, finalNum);
}//end of else
}//end of method4
/**********************************************************
* Multiply number by the power of 2 and returns the product
* @param initialNum is the input/initial integer
* @param finalNum is the final integer
* @return initialNum and finalNum as an Integer
***********************************************************/
public static int powerCount(int initialNum, int finalNum)
{
//base case
if(initialNum == finalNum){
return initialNum;
}
//recursive case
else{
return initialNum + powerCount(initialNum *2, finalNum);
}//end of else
}//end of method5
}//end of class
ここpowerCount方法であって、
public static int powerCount(int initialNum, int finalNum)
{
//base case
if(initialNum == finalNum){
return initialNum;
}
//recursive case
else{
return initialNum + powerCount(initialNum *2, finalNum);
}//end of else
}//end of method5
}//end of class
ここでは、また、それを返す/呼び出し、あなたの最大の問題は、あまりにも多くのコードです
// method call to 5th method
result2 = powerCount(1,(int)Math.pow(2,num));
// display output of 5th method to screen
System.out.println(result2);
'(問題はpowerCountメソッドです).' - よく関連するコードを投稿してください。 –
oops申し訳ありませんが、編集 – Siegfraud245
'Math.pow(x、2)'は 'x * x'ではなく' x * 2'と同じです – 4castle